0% found this document useful (0 votes)
15 views

exercises---recursion

The document provides exercises focused on identifying recursive elements in algorithms, emphasizing the importance of base cases, recursive calls, and simplifying problems. It includes several recursive algorithms with tasks to determine their output for specific inputs, as well as questions about the nature of recursion and its applications. Additionally, it discusses the structure of a method for generating anagrams and poses questions regarding its recursive elements.

Uploaded by

Ketaki Bhate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

exercises---recursion

The document provides exercises focused on identifying recursive elements in algorithms, emphasizing the importance of base cases, recursive calls, and simplifying problems. It includes several recursive algorithms with tasks to determine their output for specific inputs, as well as questions about the nature of recursion and its applications. Additionally, it discusses the structure of a method for generating anagrams and poses questions regarding its recursive elements.

Uploaded by

Ketaki Bhate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Recursion - Exercises

Identifying Recursive elements

Remember: Parts of a Recursive Algorithm

All recursive algorithms must have the following:

● Base Case (When to stop)


● The process must work toward base case
● Recursive Call (call the same process)

The "work toward base case" is where we make the problem simpler.

The recursive call, is where we use the same algorithm to solve a simpler version of
the problem.

The base case is the solution to the "simplest" possible problem (For example, the
base case to adding a list of numbers would be if the list had only one number... thus
the answer is the number).

1.Consider the following recursive algorithm, in which N is a parameter in the


method rec. The return statement gives the value that the method generates.

rec(N)
if (N<=0)
return 8
else
return N + rec(N-2)
end if

Determine the value of rec(6).

2. Consider the following recursive algorithm, in which N is a parameter in the


method rec. The return statement gives the value that the method generates.

rec(N)
if (N<=1)
return N+1
else
return N * rec(N-3)
end if

Determine the value of rec(9).


3. Consider the following recursive algorithm, in which N is a parameter in the
method rec. The return statement gives the value that the method generates.

rec(N)
if (n>10)
return N - 3;
else
return N + rec(N +2)
end if

Determine the value of rec(2).

4. Consider the following recursive algorithm, in which N is a parameter in the


method rec. The return statement gives the value that the method generates.

rec(N)
if (n > 10)
return n - 1
else
n = n + 1
return N + rec(N + 1)
end if

Determine the value of rec(0).

5. Consider the following recursive algorithm, in which N is a parameter in the


method rec. The return statement gives the value that the method generates.

rec(N)
if (N = = 2)
return 50
else if (N = = 3)
return 100
else
return (rec(N - 1) + rec(N - 2)*2)

end if

Determine the value of rec(5).


6. Consider the following recursive algorithm.
1 rec(N)
2 if(n = = 1)
3 OUPUT(“X”)
4 else if( n%2 = = 0)
5 OUTPUT(“(”)
6 rec(N-1)
7 OUTPUT(“)”)
8 else
9 OUTPUT(“<”)
10 rec(N-1)
11 OUTPUT(“>”)
12 Endif

a) Identify the base case in the algorithm.


b) Identify the recursive calls by line number.
c) If a call is made to rec 5 is the base case ever reached?

7. Which of the following is correct about recursion? Choose all that apply.

Recursion is a technique in which a function calls itself.


Recursion is a technique in which a function calls other functions.
A recursive function must be stopped with a base case at a certain point in
time. Recursion is equivalent to artificial intelligence.
Recursion is equivalent to iterative programming.
Recursion is a method of solving a problem, where the solution depends on
solutions to smaller instances of the same problem (the divide and conquer
principle).
Recursive code always performs better than iterative code.
Recursive code can be easier to read than iterative code.
It is easy to traverse a hierarchical file system with recursion.
Recursive code can be easily converted into iterative code.
8. Which of the following problems should be solved with recursion?
We have a social network of professionals (such as LinkedIn), and we would
like to find someone who has certain skills, such as Python programming.
We want to calculate the square root of a number.
We want to calculate the average of a list of numbers.
We are looking for a number in a sorted list.
We have a family tree, and we would like to print it out like a tree.
We have a list of students who attended the class, and we want to count the
number of those students.

9. Consider the following method Anagrams. It is used to print out all the possible
anagrams of a word. The method substring allows extracts to be made from an
alphanumeric variable by passing the positions of in the variable to act as the points
of extraction.
1 Anagrams(PREFIX, WORD)
2 if(WORD.length() <= 1)
3 PRINT(PREFIX + WORD)
4 else
5 loop X from 0 to WORD.length()
6 CUR = WORD.substring(i, i + 1)
7 BEFORE = WORD.substring(0, i) // letters before cur
8 AFTER = WORD.substring(i + 1) // letters after cur
9 Anagrams(PREFIX + CUR, BEFORE + AFTER)
10 end loop
11 end if

a) Identify the line where there is a recursive statement.


b) Identify the line of the base case.

You might also like