【发布时间】:2017-09-29 03:44:56
【问题描述】:
我有一个链表,我在一个范围内迭代并返回所有可以表示为该范围内整数的平方数。而不是只返回可以这样做的数字,它将返回None,例如9, None, None...,16, None, None..., 25,我希望它只返回9, 16, 25 etc etc
class Squares:
def __init__(self, start, end):
self.__start = start - 1
self.__end = end -1
def __iter__(self):
return SquareIterator(self.__start, self.__end)
class SquareIterator:
def __init__(self, start, end):
self.__current = start
self.__step = 1
self.__end = end
def __next__(self):
if self.__current > self.__end:
raise StopIteration
else:
self.__current += self.__step
x = self.__current - self.__step + 1
self.__current - self.__step + 1
if str(x).isdigit() and math.sqrt(x) % 1 == 0:
return x
【问题讨论】:
-
旁注:你认为这里的链表是什么?您在这里所做的没有一件与链表相关。
-
您是否误会了 linked list 和 iterator 这两个术语?
标签: python python-3.x list loops