Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 12: Recursion
35 problems · 0 figuresConcepts in Practice 12.1.1 §12.1 source ↗
- a.Move the small ring to the middle tower, move the bigger ring to the target tower, and move the small ring to the target tower.
- b.Move two rings from the source tower to the target tower.
- c.Cannot be solved with recursion.
Answer
Concepts in Practice 12.1.2 §12.1 source ↗
- a.Move three rings from the source tower to the target tower.
- b.Move two rings to the middle tower, then move the biggest ring to the target tower, and finally, move two rings to the target tower.
- c.Cannot be solved with recursion.
Answer
Concepts in Practice 12.1.3 §12.1 source ↗
- a.1
- b.2
- c.0
Answer
Concepts in Practice 12.1.4 §12.1 source ↗
- a.1
- b.2
- c.3
Answer
Concepts in Practice 12.1.5 §12.1 source ↗
- a.3
- b.4
- c.7
Answer
Concepts in Practice 12.1.6 §12.1 source ↗
- a.15
- b.7
- c.3
Answer
Concepts in Practice 12.2.1 §12.2 source ↗
Can the following algorithms be written recursively?
The summation of1 + 2 + 3 + . . . + (n - 1) + n where n is a positive integer.Answer
n depends on the summation to n - 1.Concepts in Practice 12.2.2 §12.2 source ↗
Can the following algorithms be written recursively?
Listing the odd numbers greater than 0 and less than a given numbern.Answer
1 .Concepts in Practice 12.2.3 §12.2 source ↗
Can the following algorithms be written recursively?
Listing the primary numbers (prime numbers) greater than 3.Answer
Concepts in Practice 12.2.4 §12.2 source ↗
Can the following algorithms be written recursively?
Listing the Fibonacci sequence of numbers.Answer
Concepts in Practice 12.2.5 §12.2 source ↗
For the questions below, the function rec_fact() is another recursive function that calculates a factorial. What is the result of each definition of rec_fact() if n = 17 is the initial input parameter?
def rec_fact(n): return n * rec_fact(n - 1)
- a.
355687428096000 - b.
0 - c.no result / infinite computation
Answer
Concepts in Practice 12.2.6 §12.2 source ↗
For the questions below, the function rec_fact() is another recursive function that calculates a factorial. What is the result of each definition of rec_fact() if n = 17 is the initial input parameter?
def rec_fact(n):
if n < 0:
print("error")
elif n == 0:
return n
else:
return n * rec_fact(n - 1)- a.
355687428096000 - b.
0 - c.no result / infinite computation
Answer
0rec_fact() uses a base case of n == 0, which works because, just like 1!, 0! = 1. However, the base case of n == 0 returns n, which at this stage is 0 , and therefore zeroes out the rest of the computation.Concepts in Practice 12.2.7 §12.2 source ↗
For the questions below, the function rec_fact() is another recursive function that calculates a factorial. What is the result of each definition of rec_fact() if n = 17 is the initial input parameter?
def rec_fact(n):
if n < 0:
print("error")
elif n == 0:
return 1
else:
return n * rec_fact(n - 1)- a.
355687428096000 - b.
0 - c.no result / infinite computation
Answer
355687428096000n == 0. 0! = 1.Concepts in Practice 12.2.8 §12.2 source ↗
For the questions below, the function rec_fact() is another recursive function that calculates a factorial. What is the result of each definition of rec_fact() if n = 17 is the initial input parameter?
def rec_fact(n):
if n < 0:
return -1
else:
return n * rec_fact(n - 1)- a.
355687428096000 - b.
0 - c.no result / infinite computation
Answer
0n == 1 is not used in the function. When n is 0 , the overall multiplication becomes 0 . The recursion begins returning once the n < 0 base case is reached, initially returning -1. But the previous recursive call would return 0 , thereby zeroing out all computations and leaving an overall result of 0 .Concepts in Practice 12.3.1 §12.3 source ↗
Refer to the animation above.
Which of the following doespalindrome() recognize as a palindrome?- a.
madamm - b.
madaM - c.
madame
Answer
madaM "madam" is a palindrome, so the function will recognize the word as a palindrome.Concepts in Practice 12.3.2 §12.3 source ↗
Refer to the animation above.
What would happen if the condition on line 4 ofpalindrome() is changed to if len(word) == 1:- a.Only palindromes with an odd number of letters would be recognized correctly.
- b.Nothing. The function would work the same.
- c.The function would not recognize any palindromes.
Answer
len(word) == 0, which is resolved with the condition len(word) <= 1.Concepts in Practice 12.3.3 §12.3 source ↗
Refer to the animation above.
What would happen if the condition in line 8palindrome() is changed to if palindrome(word.strip(word[0])).- a.Nothing. The function would work the same: the first and last letter would be removed.
- b.Some words, such as
"madamm", would be incorrectly recognized as a palindrome.
Answer
"madamm", would be incorrectly recognized as a palindrome.strip() function removes a given letter from the beginning and end of the string, including repeated letters. When strip('m') is used with "madamm" , the resultant string is "ada" .Concepts in Practice 12.3.4 §12.3 source ↗
Refer to the above animation. What would permu_check() return for each pair of lists below?
list_num = [1, 7, 99, 2] other_list = [99, 7, 1, 2]
- a.
True - b.
False
Answer
TrueConcepts in Practice 12.3.5 §12.3 source ↗
Refer to the above animation. What would permu_check() return for each pair of lists below?
list_num = [22, 9, 15, 17, 15] other_list = [17, 9, 22, 22, 15]
- a.
True - b.
False
Answer
Falselist_num has an extra 15, and other_list has an extra 22.Concepts in Practice 12.3.6 §12.3 source ↗
Refer to the above animation. What would permu_check() return for each pair of lists below?
honors_list = ["Bo", "Joe", "Sandy"] first_names_of_team_1 = ["Sandy", "Bo", "Joe"]
- a.
True - b.
False
Answer
TrueConcepts in Practice 12.3.7 §12.3 source ↗
Refer to the above animation. What would permu_check() return for each pair of lists below?
this_list = [1, 2, [3], [4], 5] that_list = [[3], 1, 5, [4], 2]
- a.
True - b.
False
Answer
TrueConcepts in Practice 12.3.8 §12.3 source ↗
Refer to the above animation. What would permu_check() return for each pair of lists below?
list_1 = [1, 2, 3] list_2 = [1, 2, 3]
- a.
True - b.
False
Answer
TrueConcepts in Practice 12.4.1 §12.4 source ↗
fib(9) results in _____.- a.
13 - b.
21 - c.
34
Answer
34fib(9) = fib(8) + fib(7). fib(9) = fib(8) + fib(7) = 21 + 13 = 34.Concepts in Practice 12.4.2 §12.4 source ↗
fib(5)?- a.2
- b.5
- c.8
Answer
Concepts in Practice 12.4.3 §12.4 source ↗
- a.tree
- b.recursion tree
- c.iteration tree
Answer
Concepts in Practice 12.4.4 §12.4 source ↗
- a.15
- b.20
- c.5
Answer
5 * 3 = 15 and 5 * 7 = 35, so 5 is a the greatest common divisor of 15 and 35 .Concepts in Practice 12.4.5 §12.4 source ↗
gcd(24, 30)?- a.4
- b.1
- c.24
Answer
gcd(24, 30) results in 30 - 24 = 6, so the next call is gcd(24, 6). Thereafter, other calls are gcd(18, 6), gcd(12, 6), and gcd (6,6) for a total of four recursive calls after the initial function call.Concepts in Practice 12.4.6 §12.4 source ↗
- a.13
- b.1
- c.23
Answer
Concepts in Practice 12.4.7 §12.4 source ↗
gcd(13, 23)?- a.1
- b.8
- c.13
Answer
gcd(13, 23) results in 23 - 13 = 10, so the next call is gcd(13, 10). Thereafter, other calls are gcd(10, 3), gcd(7, 3), gcd (4,3), gcd(3,2), gcd(2,1), and gcd(1,1) for a total of eight calls.Concepts in Practice 12.5.1 §12.5 source ↗
- a.
[5, 2, 7, 1, 8] - b.
[9, 8, 7, 6, 5] - c.
[4, 5, 6, 7, 8]
Answer
[4, 5, 6, 7, 8]Concepts in Practice 12.5.2 §12.5 source ↗
binary_search() function is called with low = 4, and high = 7, mid computes to _____.- a.
5 - b.
6 - c.
11
Answer
5mid = (4 + 7) // 2 = 11 // 2 = 5. The operator // is used for floor division.Concepts in Practice 12.5.3 §12.5 source ↗
binary_search() function occur in the animation?- a.3
- b.4
- c.8
Answer
mid being 3 , next mid becomes 5 , and finally, mid becomes 4 . The element found at index 4 is equal to the key 16 .Concepts in Practice 12.5.4 §12.5 source ↗
binary_search() function occur in the code example when searching 86?- a.4
- b.14
Answer
mid being (0 + 13) // 2 = 6. The next call has mid (7 + 13) // 2 = 10. The next call has mid (11 + 13) // 2 = 12. At this point, 86 < 99, so the final call is to mid 11, after which there is nowhere else to go.Concepts in Practice 12.5.5 §12.5 source ↗
three_towers()?- a.7
- b.15
Answer
Concepts in Practice 12.5.6 §12.5 source ↗
- a.more complex
- b.less complex
Answer
Loading chapter 13…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.