Week12 Worksheet
Week12 Worksheet
Name: EID:
Read the questions carefully, and answer each question in the space provided. Use scratch
paper to do your work and then copy your answers neatly and legibly onto the test paper.
Only answers recorded on the test paper will be graded.
1. (12 points: 1 point each) The following are true/false questions. Write either T
or F in the boxes at the bottom of page 1. If there’s any counterexample, it’s
false.
(b) A recursive function must have a base case to stop the recursion.
(c) Any recursive function using a list can be adjusted to use a set.
(d) Recursive functions are always more efficient than iterative solutions.
(e) Recursive functions can be called with different parameters in each recursive
call.
(g) Recursive functions can only call themselves once within their body.
(h) Recursion is the only way to implement functions that solve certain mathe-
matical problems efficiently, such as factorials.
(i) If function A calls function B, and function B calls function A, then neither
function is recursive because they do not directly call themselves.
(j) Recursion always leads to infinite recursive calls if not implemented correctly.
(k) Recursion is generally recommended for problems that can be easily solved
using loops.
a b c d e f g h i j k l
Questions 2-9 are multiple choice. Each counts 2 points. Write the letter of the
BEST answer in the box on the next page. Please write your answer in
UPPERCASE. Each problem has a single answer.
9. What is the correct recursive implementation for a function that takes two ordered
strings as input and returns their (ascending) ordered concatenation?
A. def ordered_concat(str1, str2):
if not str1:
return str2
elif not str2:
return str1
else:
return ordered_concat(str1[1:], str2[1:]) + max(str1[0], str2[0])
2 3 4 5 6 7 8 9
The following 8 questions require you to trace the behavior of some Python code and
identify the output of that code. For each question, write the output for the code segment
on the provided line.
10. (3 points)
def coraline(wybie):
if wybie <= 0:
return
print(wybie, end = " ")
coraline(wybie - 2)
print(wybie, end = " ")
print(coraline(5))
11. (3 points)
def lulu(jerry):
if not jerry:
return 0
elif jerry[0] in "aeiouAEIOU":
return 1 + lulu(jerry[1:])
else:
return lulu(jerry[1:])
print(lulu("Buttercup"))
12. (3 points)
def fanfiction(trope):
if len(trope) == 0:
return ""
else:
return fanfiction(trope[1:]) + trope[0]
print(fanfiction("revol2ymene"))
Page total: /9
CS303E Week 12 Worksheet: Recursion 6
13. (3 points)
def peanuts(snoopy, woodstock):
if woodstock == 0:
return snoopy
else:
return peanuts(woodstock, snoopy % woodstock)
14. (3 points)
def mystery(gang):
clues = []
for who in gang:
if type(who) == list:
clues.extend(mystery(who))
else:
clues.append(who)
return clues
15. (3 points)
def girlPower( arr, l, r, x):
if r < l:
return -1
if arr[l] == x:
return l
if arr[r] == x:
return r
return girlPower(arr, l+1, r-1, x)
Page total: /9
CS303E Week 12 Worksheet: Recursion 7
16. (3 points)
def spookyMath(scaryNums):
if scaryNums == []:
return 0
elif scaryNums[0] % 2 == 0:
return -scaryNums[0] + spookyMath(scaryNums[1:])
else:
return scaryNums[0] + spookyMath(scaryNums[1:])
17. (3 points)
Page total: /6