悬赏 1 个论坛币 未解决
Write a Python class that extends the Progression class so that each value
in the progression is the absolute value of the difference between the previous two values. You should include a constructor that accepts a pair of
numbers as the first two values, using 2 and 200 as the defaults.不知道直接贴码会不会变形。附件是progression的源码
class Progression:
def __init__(self, start = 0):
self._current = start
def _advance(self):
self._current += 1
def __next__(self):
if self._current is None:
raise StopIteration()
else:
answer = self._current
self._advance()
return answer
def __iter__(self):
return self
def print_progression(self,n):
print(' '.join(str(next(self))for j in range(n)))
[size=14.6667px]class FibonacciProgression(Progression):
[size=14.6667px] def __init__(self, first=0,second=1):
[size=14.6667px] super().__init__(first)
[size=14.6667px] self._prev = second + first
[size=14.6667px] def _advance(self):
[size=14.6667px] self._prev, self._current = (self._current, self._current-self._prev)
[size=14.6667px]