Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the following Python code and set of instructions, could I get some help please? Code has to be done in Python. Instructions: Modify the

Using the following Python code and set of instructions, could I get some help please? Code has to be done in Python.

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

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

Code is below, please use same code, thank you!

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

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

Relational Database Technology

Authors: Suad Alagic

1st Edition

354096276X, 978-3540962762

More Books

Students also viewed these Databases questions