Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can i get help with this python code pls? Create a Python file called factorial.

ID: 3802618 • Letter: C

Question

Can i get help with this python code pls?

Create a Python file called factorial.py that contains 2 functions called factorial and cachedfactorial and a single dictionary called cache. Both of the functions need to take a single integer input value. The factorial function must recursively calculate and return the factorial value of the input argument. The cachedfactorial function must also recursively calculate the factorial value, but additionally needs to save any intermediate calculation results in the cache dictionary and terminate early if a required value can be found in the cache. For example, upon completion of cachedfactorial(5), which computes the value of 5! (5*4*3*2*1), the cache should have stored the values of 5!, 4!, 3!, 2!, and 1!. If you try to compute the value of 7! (i.e., 7*6*5*4*3*2*1) afterward, you should not need to recursively compute the 5*4*3*2*1 part again.

Explanation / Answer

Here is the code for you:

#!/usr/bin/python
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)

def memoize(f):
cache = {}
def decorated_function(*args):
if args in cache:
return cache[args]
else:
cache[args] = f(*args)
return cache[args]
return decorated_function

@memoize
def cachedFactorial(n):
return 1 if n == 0 else n * cachedFactorial(n-1)

print cachedFactorial(100)