Problem 1A. 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 2Make 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:*edecdebcdeabcde Method call rec_string(‘abc’), will produce the following output:*cbcabc 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.

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.

 

  1. 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.

 

 

check_circle

Expert Answer

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
help_outline

Image Transcriptionclose

Python 3.7.4 Shell File Edit Shell Debug Options Window Help RESTART: C:/Users/Atul/Desktop/test.py 27 >>> /TToaro /Atwl /nao n/taot DFSTADT

fullscreen
Step 3

b.

Complete python program:

#Recursive Function foo1() which accepts a parameter n

def foo2(n):

    if n>0:

        print(n,end='')

      &nbs...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

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...

question_answer

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...

question_answer

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...

question_answer

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 ...

question_answer

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...

question_answer

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 ...

question_answer

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...

question_answer

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)...

question_answer

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...