In: Computer Science
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10))
2 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(n, m): if n < 0 and m < 0: return n * m print(n, m) if m > n: return sequence(m, n) return sequence(n - 1, m) print(sequence(1, 0))
Q1.
def sequence(a, b, c):
if a < b < c:
return a + b + c
if a >= b:
return sequence(a - 1,
b, c)
if a >= c:
return sequence(c, b,
a)
if b >= c:
return sequence(c, b, a
+ 2)
return 0
print(sequence(10, 10, 10))
Stack diagram for the code above:
Output:
30
Q2.
def sequence(n, m):
if n < 0 and m < 0:
return n * m
print(n, m)
if m > n:
return sequence(m,
n)
return sequence(n - 1, m)
print(sequence(1, 0))
Stack diagram for the code above: