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))
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.
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 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. 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? 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; }
class Population: def __init__(self, m=0, n=0, k=0): # 1 self.m = m self.n = n self.k = k self.encounter = 0 bac = [] bac = bac + [1] * self.m + [2] * self.n + [3] * self.k self.bac = bac def plasmids(self): # 2 set = (self.m, self.n, self.k) return set def __str__(self): # 3 return "type I: {}, type II: {}, type III: {} (after {} encounters)".format(self.m, self.n, self.k, self.encounter) def __repr__(self): # 4 return "Population({}, {},...
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):...
Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...
in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...
17. For the following program, draw a stack to find the output. public class RecApp public static void main(String[] args) System.out.println(Sum(7)); public static int Sum(int n) if (n < 1) return 5; else return n + Sum (n-2); Output: (13) Stack: (4%) Sum (_ ) Sum(_) Sum(_) Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Sum( ) 7 + Sum (5) Sum (7) Write the recursive equation (Tn)) for the run time of...
Show the source code if possible or code writen with output please and thank you <3 6. Is the following a legal Python program? def proc(x, y): return 2*x + y*y def main(): print(proc(5)) main() 7. Is the following a legal Python program? def proc(x): return 2*x def main(): print(proc(5, 4)) main() 8. Is the following a legal Python program? def proc(x): print(2*x*x) def main(): proc(5) main()