Ch. 11 Recursion Python
Which of the following strings is a palindrome?
"B"
What is displayed by the following code segment? def mystery(n) : if n == 0 : return 0 else : return n % 10 + mystery(n // 10) print(mystery(0))
0
Consider the following recursive code snippet: 1. def mystery(n, m) : 2. if n == 0 : 3. return 0 4. if n == 1 : 5. return m 6. return m + mystery(n - 1, m) What value is returned from a call to mystery(1, 5)?
5
Consider the function powerOfTwo shown below: 1. def powerOfTwo(n) : 2. if n == 1 : 3. return True 4. elif n % 2 == 1 : 5. return False 6. else : 7. return powerOfTwo(n / 2) How many recursive calls are made from the original call powerOfTwo(64) (not including the original call)?
6
What is displayed by the following code segment? def mystery(n) : if n == 0 : return 0 else : return n % 10 + mystery(n // 10) print(mystery(123))
6
Consider the recursive function myPrint: 1. def myPrint(n) : 2. if n < 10 : 3. print(n) 4. else : 5. m = n % 10 6. print(m) 7. myPrint(n / 10) What is printed for the call myPrint(8)?
8
How many recursive calls to the fib function shown below would be made from an original call to fib(4)? (Do not count the original call) 1. def fib(n) : 2. # assumes n >= 0 3. if n <= 1 : 4. return n 5. else : 6. return fib(n - 1) + fib(n - 2)
8
Which of the following options could be used as a terminating condition for a recursive function that finds the middle character of a String with any number of characters? I. the length of the String is 1 II. first and last String characters match III. the String is not empty
I
What is required to make a recursive function successful? I. One or more special cases that handle the simplest computations directly II. A recursive call to a smaller or simpler version of the problem III. Mutually recursive calls
I and II only
Recursion will take place if any of the following happen: I. function A calls function B, which calls function C II. function A calls function B, which calls function A III. function A calls function A
II and III
Consider the following recursive function: def myPrint(n) : if n < 10 : print(n) else : m = n % 10 print(m) myPrint(n // 10) What does this function do?
It prints a positive value backward, digit by digit
How many permutations are in a 5 letter word?
120
Consider the following recursive function: def myPrint(n) : if n < 10 : print(n) else : m = n % 10 print(m, end="") myPrint(n // 10) What is printed for the call myPrint(821)?
128
Consider the following recursive code snippet: 1. def mystery(n, m) : 2. if n == 0 : 3. return 0 4. if n == 1 5. return m 6. return m + mystery(n - 1, m) What value is returned from a call to mystery(3,6)
18
Consider the following code snippet: def isEven(num) : if num % 2 == 0 : return True else : return isOdd(num) def isOdd(num) : if num % 2 == 1 : return True else : return isEven(num) For any given value of num, what is the maximum number of function calls that could occur?
2
Recall the permutations function from Section 11.5 in the textbook. def permutations(word) : result = [] if len(word) == 0 : result.append(word) return result else: for i in range(len(word)) : shorter = word[ : i] + word[i + 1 : ] shorterPermutations = permutations(shorter) for string in shorterPermutations : result.append(word[i] + string) return result How permutations will be generated by calling permutations("code")?
24
A unique permutation is one that is different from any other generated permutation. How many unique permutations does the string "bee" have?
3
Consider the following code segment: def f1(n) : if n < 0 : return 0 if n % 2 == 1 : return n return f2(n + 1) def f2(n) : if n < 0 : return 0 if n % 2 == 0 : return n return f1(n // 2) print(f2(7)) When this code is run, it will display:
3
Given the following code: def main() : recurse(3) def recurse(n) : total = 0 if n == 0 : return 0 else : total = 3 + recurse(n - 1) print(total) return total main() What values will be printed when this code is executed?
3, 6, and 9
What is displayed when the following program executes? def mystery(s) : return s[len(s) - 1] + mystery(s[0 : len(s) - 1]) mystery("123")
321
Given the following code: def main() : recurse(4) def recurse(n) : total = 0 if n == 0 : return 0 else : total = 4 + recurse(n - 2) print(total) return total main() What values will be printed when this code is executed?
4 and 8
Consider the following code segment for computing Fibonacci numbers. How many times is fib(3) computed in order to compute fib(7)? def fib(n) : if n <= 2 : return 1 else : return fib(n - 1) + fib(n - 2)
5
Consider the following code segment: def f1(n) : if n < 0 : return 0 if n % 2 == 1 : return n return f2(n + 1) def f2(n) : if n < 0 : return 0 if n % 2 == 0 : return n return f1(n // 2) print(f1(10)) When this code is run, it will display:
5
What is displayed by the following code segment? def mystery(n) : if n == 0 : return 0 else : return n % 10 + mystery(n // 10) print(mystery(-1))
A runtime error is reported
Which of the following statements about palindromes is correct?
All strings of length 0 or 1 are palindromes.
Which statement about backtracking is correct?
Backtracking builds up partial solutions that get increasingly closer to the goal.
What library is used to load a webpage into a data structure that makes it easy to extract the page's tags and the information between the tags?
Beautiful Soup
Why does the best recursive function usually run slightly slower than its iterative counterpart?
Each recursive function call takes processor time.
Which problem is well suited to being solved with mutually recursive functions?
Evaluating a numeric expression
Which problem is well suited to being solved with a backtracking algorithm?
Finding a solution to the eight queens problem
Which of the following statements about recursion is correct?
In many cases, a recursive solution will be easier to understand and to implement than an iterative solution.
What term is used to describe a function that keeps on calling itself until the program crashes?
Infinite Recursion
Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength <= 0 : 3. return 0 4. if sideLength == 1 : 5. return 1 6. smallerSideLength = sideLength - 1 7. smallerArea = triangleArea(smallerSideLength) 8. area = smallerArea + sideLength 9. return area What will happen if line #6 is changed to: smallerSideLength = sideLength
Infinite recursion will occur when sideLength is greater than or equal to 2.
Consider the following code segment: def mystery(s, c) : if len(s) == 0 : return False elif s[i] == c : return True else : return mystery(s[1 : ], c) What does the mystery function do?
It determines whether or not c occurs in s
Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength <= 0 : 3. return 0 4. if sideLength == 1 : 5. return 1 6. smallerSideLength = sideLength - 1 7. smallerArea = triangleArea(smallerSideLength) 8. area = smallerArea + sideLength 9. return area What will happen if lines 4 and 5 are replaced with the following lines of code? if sideLength == 1 : return 2
It would increase the result calculated by the function for all calls except those where sideLength <= 0
Consider the following code segment: def fib(n) : # Line 1 if n <= 2 : return 1 # Line 2 else : return fib(n - 1) + fib(n - 2) # Line 3 print(fib(6)) # Line 4 Which line is the base case for this recursive function?
Line 2
Consider the following code segment: def mysteryPrint(n) : if n == 0 : return 0 else : return n + mysteryPrint(n - 1) What is returned for the call mysteryPrint(-4)?
Nothing. The program crashes with a runtime error.
Consider the function powerOfTwo shown below: 1. def powerOfTwo(n) : 2. if n == 1 : 3. return True 4. elif n % 2 == 1 : 5. return False 6. else : 7. return powerOfTwo(n / 2) What is the best interpretation of lines 2 and 3?
One is a power of two.
What is the purpose of a recursive helper function?
Shield the user of the recursive function from the recursive details
Recall the permutations function from Section 11.5 in the textbook. def permutations(word) : result = [] if len(word) == 0 : result.append(word) return result else: for i in range(len(word)) : shorter = word[ : i] + word[i + 1 : ] shorterPermutations = permutations(shorter) for string in shorterPermutations : result.append(word[i] + string) return result What is the base case for this function?
The empty string
Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength <= 0 : 3. return 0 4. if sideLength == 1 : 5. return 1 6. smallerSideLength = sideLength - 1 7. smallerArea = triangleArea(smallerSideLength) 8. area = smallerArea + sideLength 9. return area What will happen if lines #2 and #3 are replaced with the following code segment? if sideLength <= 0 : return sideLength
The function will still return correct results for all triangles with non-negative side lengths.
Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function: 1. def myFactorial(n) : 2. if n == 1 : 3. _____________________________ 4. else : 5. return n * myFactorial(n - 1)
return 1
Consider the following function: 1. def mystery(n, m) : 2. if n == 0 : # special case 1 3. return 0 4. if n == 1 : # special case 2 5. return m 6. return m + mystery(n - 1), m) What will happen if lines #2 and #3 were swapped with lines #4 and #5?
The original function and the modified function will return the same result for all integer values of n and m.
Recall the permutations function from Section 11.5 in the textbook. def permutations(word) : result = [] if len(word) == 0 : result.append(word) return result else: ____________________ shorter = word[ : i] + word[i + 1 : ] shorterPermutations = permutations(shorter) for string in shorterPermutations : result.append(word[i] + string) return result In the textbook, the line now represented by the blank was for i in range(len(word)) :. What would happen if the blank was filled in with for i in range(len(word) - 1, -1, -1) :
The same permutations of the word would be generated in the reverse order
Consider the following code segment: def sumList(data) : return sumListIndex(data, 0) def sumListIndex(data, i) : if i == len(data) : return 0 else : return data[i] + sumListIndex(data, i + 1) This code segment contains an example of
a recursive helper function
Recall the backtracking strategy outlined in the textbook. A similar strategy can be used to solve Sudoku puzzles. def solve(gameBoard) : status = examine(gameBoard) if status == CONTINUE : for nextBoard in extend(gameBoard) : solve(nextBoard) elif status == ACCEPT: ____________________ What code should be placed in the blank to complete the solution to this problem?
print(gameBoard)
Consider the following code segment: ____________________ if x % y == 0: return y else: return gcd(y, x % y) Which statement should be placed in the blank to complete the recursive function definition?
def gcd(x, y) :
Which statement finds all of the links in a webpage?
elements = doc.find_all("a")
Consider the following code snippet for recursive addition: 1. def add(i, j) : 2. # assumes i >= 0 3. if i == 0 : 4. return j 5. else : 6. return add(i - 1, j + 1) Identify the terminating condition in this recursive function.
if i == 0 :
Consider the code for the recursive function myPrint shown in this code snippet: 1. def myPrint(n) : 2. if n == 0 : 3. return 0 4. else : 5. return n + mysteryPrint(n - 1) To avoid infinite recursion, which of the following lines of code should replace the current terminating case?
if n <= 0
The following code segment is supposed to determine if an integer, n, is even or odd using mutual recursion. def isEven(n) : if n == 0 : return True else : return isOdd(abs(n) - 1) def isOdd(n) : ____________________ return False else : return isEven(abs(n) - 1) What code should be placed in the blank to achieve this goal?
if n == 0 :
Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength <= 0 : 3. return 0 4. if sideLength == 1 : 5. return 1 6. smallerSideLength = sideLength - 1 7. smallerArea = triangleArea(smallerSideLength) 8. area = smallerArea + sideLength 9. return area Where is/are the terminating condition(s)?
lines #3 and #5
The printList function is supposed to print all of the elements in a list, one per line. What line of code should be placed in the blank to achieve this goal? def printList(data) : ____________________ def printHelper(data, i) : if i == len(data) : return print(data[i]) printHelper(data, i + 1)
printHelper(data, 0)
Consider the following code segment: def f1(n) : if n < 0 : return 0 if n % 2 == 1 : return n return f2(n + 1) def f2(n) : if n < 0 : return 0 if n % 2 == 0 : return n return f1(n // 2) This code segment would be classified as:
mutually recursive
The following code segment is supposed to determine whether or not a string is a palindrome, meaning that it is the same forward and backward. def isPalindrome(s) : return palindromeHelper(s, l, h) def palidromeHelper(s, l, h) : if h <= l : return True if s[l] != s[h] : return False ____________________ What line of code should be placed in the blank to achieve this goal?
palindromeHelper(s, l + 1, h - 1)
Assume that the address variable currently holds the URL for webpage. Which statement(s) store a structured representation of that webpage in the doc variable?
response = urllib.request.urlopen(address) doc = bs4.BeautifulSoup(response)
The following function is supposed to use recursion to compute the area of a square from the length of its sides. For example, squareArea(3) should return 9. def squareArea(sideLength) : if sideLength == 1 : return 1 else : ____________________ What line of code should be placed in the blank to achieve this goal?
return 2 * (sideLength - 1) + squareArea(sideLength - 1)
Consider the following code segment: def f(n) : if n == 1 : return n elif n == 2 : ____________________ else : return f(n // 2) def g(n) : if n % 2 == 0 : return n - 1 else : return f(n - 1) What statement should be placed in the blank to make these functions mutually recursive?
return g(n - 1)
Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function: 1. def myFactorial(n) : 2. if n == 1 : 3. return 1 4. else : 5. _______________________
return n * myFactorial(n - 1)
The following code segment is supposed to sum the numbers from 1 up to and including n using recursion. def sumOneToN(n) : if n == 0 : return 0 ____________________ Which statement should be placed in the blank to achieve this goal?
return n + sumOneToN(n - 1)
A recursive computation solves a problem using the solution to the same problem with ____________________ inputs.
simpler
In recursion, the terminating condition is analogous to a loop _________.
termination condition
A palindrome is a word or phrase spelled which reads the same forward or backward. Consider the following code snippet: def palindrome(s) : return isPal(s, 0, len(s) - 1) def isPal(s, left, right) : if left >= right : return True elif s[left] == s[right] : return isPal(s, left + 1, right - 1) else : return False What does the function palindrome return?
the Boolean value returned from the isPalfunction
Consider the triangleArea function from the textbook shown below: 1. def triangleArea(sideLength) : 2. if sideLength <= 0 : 3. return 0 4. if sideLength == 1 : 5. return 1 6. smallerSideLength = sideLength - 1 7. smallerArea = triangleArea(smallerSideLength) 8. area = smallerArea + sideLength 9. return area Assume that line #5 is changed to this: smallerArea = triangleArea(sideLength) This would cause infinite recursion for __________________________________
triangles with sideLength greater than or equal to 1