make a function that performs an iterative approach of g(n). So, g_iter(n) shoul
ID: 3853140 • Letter: M
Question
make a function that performs an iterative approach of g(n). So, g_iter(n) should perform same operation as g(n), but in a iterative approach(non recursive)
def g(n): Return the value of G(n), computed recursively. 1 2 >g(3) >g(4) 10 >>>from construct_check import check >>check(HW_SOURCE_FILE, 'g',I'While','For']) True return In return g(n 1) + 2g(n 2) +3 * g(n 3) def g_iter(n): Return the value of G(n), computed iteratively. >g iter(1) 1 >g_iter(2) >>>g_iter(3) 3 >>g_iter(4) 10 s>>giter(5) >>> from construct_check import check >>>check (HW_SOURCE_FILE, 'g iter', ['Recursion']) TrueExplanation / Answer
We can use dynamic approach of stored programing
def g_iter(n):
arr = [0 for x in range(n+1)]
for i in range (1,n+1):
if i <= 3:
arr[i] = i
else:
arr[i] = arr[i-1] * arr[i-2] * arr[i-3]
print arr[n-1]
I hope this helps you :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.