Question

Asked Nov 12, 2019

39 views

**Problem 1**

A. Consider the following recursive method.

**def foo1(n):
**

** if n == 2: **

** return 1**

** else:**

** return 3 * foo1(n – 1) **

What value is returned as a result of the call **foo1(5)**? **Show **the recursive calls generated.

B. Consider the following recursive method.

**def foo2(n):**

**
if n > 0: **

** print(n, end=’ ‘) **

** foo2(n-1) **

What is the output of the call **foo2(5)**

**Problem 2**

Make sure your code is readable and well-documented. Each function must also begin with a title block that describes the task of the function, input parameters and return value.

- Write a recursive function that takes a string as an input and returns the reverse of the string.

2. Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function.

Method call rec_string(‘abcde’), will produce the following output:

*

e

de

cde

bcde

abcde

Method call rec_string(‘abc’), will produce the following output:

*

c

bc

abc

3. Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an important problem that arises in many commercial applications. We can efficiently compute the gcd using the following property, which holds for positive integers p and q:

If p > q, the gcd of p and q is the same as the gcd of q and p % q.

Step 1

**a.**

**Complete Python Program: **

**#**Recursive Function foo1() which accepts a parameter n

def foo1(n):

if n==2:

return 1

else:

return 3*foo1(n-1)

#print the output for n=5

print(foo1(5))

This python program prints output 27.

Function calls use stack data structure. Stacks are based on First IN last OUT mechanism.

The function foo1(n) is called first for n=5.

PUSH foo1(5) to stack,

Next, foo1(5) calls foo1(4), therefore

PUSH foo1(4) to stack

Similarly,

PUSH foo1(3) to stack

PUSH foo1(2) to stack

The recursive function calls stop here since foo1(2) returns 1. Now all functions are Popped from stack in LIFO order.

POP foo1(2) = 1

POP foo1(3) = 3 * foo1(2) = 3 * 1 = 3

POP foo1(4) = 3 * foo1(3) = 3 * 3 = 9

POP foo1(5) = 3 * foo1(4) = 3 * 9 = 27

Step 2

Step 3

**b.**

**Complete python program:**

#Recursive Function foo1() which accepts a parameter n

def foo2(n):

if n>0:

print(n,end='')

&nbs...

Tagged in

Find answers to questions asked by student like you

Show more Q&A

Q: What does it mean to choose numbers at random

A: RandomRandom is the word in which express that some item, number, quantity, etc. are chosen from the...

Q: Do you agree or disagree with Asra Taylors ideas for reform of the existing internet?

A: Asra Taylors idea for reforming existing internetYes, I do agree with Asra Taylors idea for reformin...

Q: Write a Java program that prompts the user for the page size used in a virtual memory system; this w...

A: A Java program that accepts a valid system page size and virtual address and calculates the page num...

Q: In C++: Examine the code segment. What will be printed? If there are any errors indicate the nature ...

A: The code given in question will give two compile time errors. Both the errors are indicated in bold ...

Q: WRITE A SHORT NOTE ON THE PRIMITIVE COMPONENTS OF BITCOIN VALUE CHAIN?

A: Bitcoin Value Chain can be defined with the help of three terms BlockChain, Private Keys and third M...

Q: I have to code the game mastermind, but i only got halfway. I got the user input, 8 colors and the 3...

A: solving the duplication problem:Create a temporary array named "temp" and initiate that array with ...

Q: write a c++ program: Create an array with 11 integers, which will be randomly selected from the rang...

A: In the CPP program crate an array with 11 sizes.The array elements are integer types.The elements ar...

Q: Design a reasonably efficient algorithm for solving each of the following problems and determine its...

A: AlgorithmLet x be one of the numbers and the other number is n-x. The first step is to maximize f(x)...

Q: Siobhan’s laptop has 8GB of RAM. Her operating system takes up 2GB, and her application programs use...

A: Random Access Memory (RAM):RAM is a memory device used for storage purposes.The data stored in the m...