Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me with this code? Python The Fibonacci sequence is, by definition, the integer sequence in which every number after the first

Can someone please help me with this code? Python

The Fibonacci sequence is, by definition, the integer sequence in which every number after the first two is the sum of the two preceding numbers.

In the fib.py file, complete the following:

Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapter.

  • Form a base case for any number in the sequence, except for the first and second, is the sum of the previous two

    A base case in a recursive function tells the function when to stop (to avoid going into an infinite loop)

  • The function should expect a dictionary as an additional argument. The top-level call of the function receives an empty dictionary.
  • The functions keys and values should be the arguments and values of the recursive calls.
  • Use the counter object discussed in this chapter to count the number of recursive calls.

    Counter is already provided in the main() method

  • To test your program run the main() method in the fib.py file.

Your program's output should look like the following:

 n fib 2 1 4 3 8 21 16 987 32 2178309

( Below Code is already given which needs modification )

def fib(n, table):

"""Fibonacci function with a table for memoization."""

if :

else:

# Attempt to get values for n - 1 and n - 2

# from the table

# If unsuccessful, recurse and add results to

# the table

def main():

"""Tests the function with some powers of 2."""

problemSize = 2

print("%4s%12s" % ("n", "fib"))

for count in range(5):

print("%4d%12d" % (problemSize, fib(problemSize, {})))

problemSize *= 2

if __name__ == "__main__":

main()

( Here is The code that works and gives out comes BUT Zero Credit because it doesn't follow given instructions above )

n = int(input())

D = {}

def fib(n):

if n in D:

return D[n]

if n <= 2:

D[n] = 1

return D[n]

else:

D[n] = fib(n - 1) + fib(n - 2)

return D[n]

# Code Given below is to display the output as asked by you

width = 8

print("n\t", "fib")

i = 2

while i <= n:

print(str(i).ljust(width, ' '), str(fib(i)))

i *= 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions

Question

5. Discuss the role of the Web in career management.

Answered: 1 week ago

Question

4. Design a career management system.

Answered: 1 week ago