In: Computer Science
In Python
Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the 'nxt' method. In this class, the 'val' member is a Fibonacci number. The 'nxt' method will return a 'Fibonacci' object whose value is the next number in Fibonacci series.
class Fibonacci ():
"""A Fibonacci number.
>>>a = Fibonacci():
>>>a
0
>>>a.nxt()
1
>>>a.nxt().nxt()
1
>>>a.nxt().nxt().nxt()
2
>>>a.nxt().nxt().nxt().nxt()
3
>>>a.nxt().nxt().nxt().nxt().nxt()
5
>>>a.nxt.nxt().nxt().nxt().nxt().nxt()
8
""" def __init__(self):
self.val = 0
def nxt(self):
"""YOUR SOURCE CODE HERE"""
def __repr__(self):
return str(self.val)
HINT: A new 'Fibonacci' object is needed to create and assign 'val' and 'pre' members within 'nxt' method
class Fibonacci():
    """A Fibonacci number.
    >>>a = Fibonacci():
    >>>a
    0
    >>>a.nxt()
    1
    >>>a.nxt().nxt()
    1
    >>>a.nxt().nxt().nxt()
    2
    >>>a.nxt().nxt().nxt().nxt()
    3
    >>>a.nxt().nxt().nxt().nxt().nxt()
    5
    >>>a.nxt.nxt().nxt().nxt().nxt().nxt()
    8
    """
    def __init__(self):
        self.val = 0
        self.next_val = 1
    def nxt(self):
        result = Fibonacci()
        result.val = self.next_val
        result.next_val = self.val + self.next_val
        return result
    def __repr__(self):
        return str(self.val)
# Testing the class here. ignore/remove the code below if not required
a = Fibonacci()
print(a)
print(a.nxt())
print(a.nxt().nxt())
print(a.nxt().nxt().nxt())
print(a.nxt().nxt().nxt().nxt())
print(a.nxt().nxt().nxt().nxt().nxt())
print(a.nxt().nxt().nxt().nxt().nxt().nxt())
