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

Consider the following program which includes functions that call other function

ID: 3916866 • Letter: C

Question

Consider the following program which includes functions that call other functions in a confusing way:

At the end, the program prints out four numbers by calling one of the functions defined above. The goal is two call functions in the right order such that the output has the numbers "1, 2, 3, 4" in that order.

First function call:                            [ Select ]                       ["b", "a", "c"]      

Second function call:                            [ Select ]                       ["c", "a", "b"]      

Third function call:                            [ Select ]                       ["b", "c", "a"]      

Fourth function call:                            [ Select ]                       ["c", "b", "a"]      

options c, b, a

Explanation / Answer

# Answer: b c c a

def a(x):

return x

def b(y):

return a(y) + a(1)

def c(z):

if z < 3:

return a(b(a(b(z))))

else:

return b(z) - a(2)

print(b(0)) # Should print "1"

# a(0) + a(1) = 0 + 1 = 1

print(c(0)) # Should print "2"

# a(b(a(b(z)))) = a(b(a(1))) = a(b(1)) = a(2) = 2

print(c(4)) # Should print "3"

# b(z) - a(2) = (a(4) + a(1)) - 2 = 4 + 1 - 2 = 3

print(a(4)) # Should print "4"

# 4 right away