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

(Python) For all of them, the signature is supposed to be Natural-Number -> Natu

ID: 3683437 • Letter: #

Question

(Python) For all of them, the signature is supposed to be Natural-Number -> Natural-Number

For each of these functions, answer the following questions.

a) What happens when you run the function with the input 4?

b) If it crashes, explain why. If it doesn't crash, how many times was the function called?

c) For which values will the function return an answer, and for which will it crash? For these functions, you should only worry about natural numbers: 0, 1, 2, 3, ...

d) If there are values that the function won't crash for, explain what the output will be in all those cases.

Explanation / Answer

def f1(x):#This function doesn't have terimination condition ie means it should return some value, it calls the infinite times
return f1(x)

def f2(x):#This function doesn't have terimination condition ie means it should return some value, it calls the infinite times
return f2(x-1)

def f3(x):#This function have terimination condition ie means it should return some value, it calls the infinite times
if x == 0:
return f3(x-1)
else:
return f3(x-1)

def f4(x):#This function run correctly because it returning some some value when it meet the condition
if x == 0:
return 0
else:
return f4(x-1)

def f5(x):#This function waas corrected by me,it return 0 when x>=2 return 0
if x == 0 :# if i=1 then this return f5(-1) like that it will goes f5(-3),f5(-5)... so on.
return 0
else:
return f5(x-2)

def f6(x):#This function run correctly which give summation of two's, x times
if x == 0:
return 0
else:
return 2 + f6(x-1)

def f7(x):#This function calc sum from 0 to x
if x == 0:
return 0
else:
return x + f7(x-1)
  
for i in range(4):
   #print f1(i);
   #print f2(i);
   #print f3(i);
   print "f4 ",f4(i);
   print "f5 ",f5(i+2);
   print "f6 " ,f6(i);
   print "f7 " ,f7(i);