Question

In: Computer Science

In Python Write a Fibonacci class to calculate next number in the 'Fibonacci' class by the...

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

Solutions

Expert Solution

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())


Related Solutions

( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element...
PLEASE WRITE IN PYTHON A sequence of integers is said to be Fibonacci-like if each element of the sequence (except the first two elements) is the sum of the previous two integers in the sequence. For example, the sequence 10, 14, 24, 38, 62, 100, 162, 262 is Fibonacci-like. Note that the first two integers in the above sequence are arbitrary. Each of the remaining integers is the sum of the two integers just before it in the sequence. For...
Fibonacci Write pseudocode to calculate F(n) Write pseudocode to construct a vector of the first n...
Fibonacci Write pseudocode to calculate F(n) Write pseudocode to construct a vector of the first n numbers in the Fibonacci sequence. Write a function: fibo(n = 1){ # Your code here } which takes one parameter ​n​ and returns the ​nth​ fibonacci number. Use the function you wrote to calculate the 58th Fibonacci number. Include your R code in your report The Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...) is defined as F(1) =...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas...
Please write the following Python program. Also, show all output work. Computing the Fibonacci and Lucas Series¶ Goal:¶ The Fibonacci Series is a numeric series starting with the integers 0 and 1. In this series, the next integer is determined by summing the previous two. This gives us: 0, 1, 1, 2, 3, 5, 8, 13, ... We will write a function that computes this series – then generalize it. Step 1¶ Create a new module series.py in the session02...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT