Question

Draw a stack diagram to show how the following code is executed and write the generated...

Draw a stack diagram to show how the following code is executed and write the generated output.

def sequence(n, m):
    if n < 0:
        return 1
    elif n + m > 5:
        return sequence(n, m - 1)
    else:
        return n * sequence(m - 1, n + 1)

print(sequence(5, 3))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Draw a stack diagram to show how the following code is executed and write the generated output:

Stack diagram:

• Main method will call sequence(5, 3).

• n + m > 5 → 8 > 5 is true.

return sequence(n, m-1) → return sequence(5, 2)

• n + m > 5 → 7 > 5 is true.

return sequence(n, m-1) → return sequence(5, 1)

• n + m > 5 → 6 > 5 is true.

return sequence(n, m-1) → return sequence(5, 0)

• n + m > 5 → 5 > 5 is false.

return n * sequence(m-1, n+1) → 5 * sequence(0-1, 5+1)

→ 5 * sequence(-1, 6) [Here, n = -1, m = 6]

→ 5 * 1

→ 5

• Return output sequence(5, 3) = 5

Provided code:

def sequence(n,m):

if n<0:

return 1

elif n + m > 5:

return sequence(n,m-1)

else:

return n * sequence(m-1,n+1)

print(sequence(5,3))

Output: 5

Output of the above code is integer value 5.

Add a comment
Know the answer?
Add Answer to:
Draw a stack diagram to show how the following code is executed and write the generated...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(n, m):...

    Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(n, m): if n < 0: return 1 elif n + m > 5: return sequence(n, m - 1) else: return n * sequence(m - 1, n + 1) print(sequence(5, 3))

  • Consider the following code, and write the sequence of statements (Si) that will be executed for...

    Consider the following code, and write the sequence of statements (Si) that will be executed for the values of (x,y) = (0,0), (0,1), (1,0), (1,1), (-1,-1) S1 if x==0 and y==0: S2 elif x>0 and y>0: S3 elif x>0 or y>0: S4 else: S5 S6

  • B) draw the runtime stack fall the steps (a) write the output of the following program....

    B) draw the runtime stack fall the steps (a) write the output of the following program. The output must include everything that a computer may produce on its screen, including the prompts, input, output, and their positions. Suppose the input integers are 1 and 2, in this order. (b) Draw the run time stack for every step of execution on the next page. You need to mark the return address(es) yourself. #include <iostream> using namespace std; int a; int b;...

  • c programming What is the output, to the console, once the following code has been executed?...

    c programming What is the output, to the console, once the following code has been executed? int collatz(int value) { if(value % 2 == 0) return value / 2; else return value * 3 + 1; } int main() { int currentValue = 1: int 1; for (i = 0; i < 5; i++) { printf("%d\n", currentValue); currentValue = collatz(currentValue); } return 0; }

  • Write the following program to be executed on the FPGA board. 1. Write a VHDL model...

    Write the following program to be executed on the FPGA board. 1. Write a VHDL model for a code detector as shown in the Figure. The keypad is used to unlock a door. Pressing the start button followed by the sequence red-green-red-blue unlocks the door, no other sequence can open the door. Assume the clock is slowed down and each pressing of a button is detected once. For example when red is pressed it is only detected as pressed for...

  • python programming: Can you please add comments to describe in detail what the following code does:...

    python programming: Can you please add comments to describe in detail what the following code does: import os,sys,time sl = [] try:    f = open("shopping2.txt","r")    for line in f:        sl.append(line.strip())    f.close() except:    pass def mainScreen():    os.system('cls') # for linux 'clear'    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print(" SHOPPING LIST ")    print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")    print("\n\nYour list contains",len(sl),"items.\n")    print("Please choose from the following options:\n")    print("(a)dd to the list")    print("(d)elete from the list")    print("(v)iew the...

  • The goal of this code is to draw a smiley face and be able to change emotion, mouth and eyes. Use...

    The goal of this code is to draw a smiley face and be able to change emotion, mouth and eyes. Use the demo code below to fill in the blanks ( ), as well as add any other methods or data that is needed. All blanks should be completed with a single line of code (or partial line). You should not modify any other code that exists. Only remove the blanks and add other missing methods. Use a radius of...

  • CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case....

    CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...

  • how to make my code of python work probely my q is how to write a...

    how to make my code of python work probely my q is how to write a code to find the average actual water level, average error by using python and my program should be flexible where user can select the column no. If you can see in the main program, it asked which column to know the maximum or average, the column number is passed to the function. The function should look which column to do. #THIS PROGRAMMING IS ABLE...

  • Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...

    Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None    def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT