Problem 4: Next Fibonacci Object (0pts)
Implement the next
method of the Fib
class. For this class, the value
attribute is a Fibonacci number. The next
method returns a Fib
instance whose value
is the next Fibonacci number. The next
method should take only constant time.
You can assume we will neither create Fib
instance with non-zero value
directly nor edit the value
of Fib
instance outside the class.
Hint: Keep track of the previous number by setting a new instance attribute inside next. You can create new instance attributes for objects at any point, even outside the
__init__
method.
class Fib:
"""A Fibonacci number.
>>> start = Fib()
>>> start.value
0
>>> start.next().value
1
>>> start.next().next().value
1
>>> start.next().next().next().value
2
>>> start.next().next().next().next().value
3
>>> start.next().next().next().next().next().value
5
>>> start.next().next().next().next().next().next().value
8
>>> start.value # Ensure start isn't changed
0
"""
def __init__(self, value=0):
self.value = value
def next(self):
"*** YOUR CODE HERE ***"