1 |
h13 |
CS8 M19-B |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section |
||
Optional: name you wish to be called if different from name above. | ||||
Optional: name of "homework buddy" (leaving this blank signifies "I worked alone" |
h13: Perkovic 10.1 (Introduction to Recursion - up to Practice Problem 10.3)
ready? | assigned | due | points |
---|---|---|---|
true | Tue 09/03 02:00PM | Tue 09/10 02:00PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)
READING ASSIGNMENT
Please read Perkovic 10.1 (Introduction to Recursion - up to Practice Problem 10.3). Then complete these problems and turn in your completed homework in lecture on the due date.
- (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
-
(10 pts) What is the significance of a “base case” in a recursive function?
-
On page 332, the author talks about two important properties of recursive functions:
- There exist one or more base cases which provide the stopping condition for the functions
- One or more recursive calls on arguments that are “closer” to the base cases (progress to the base case)
For each of the following implementations of
count(n)
, indicate which of the two properties are satisfied by circling the appropriate option. Then write the output when the function is called ascount(4)
. If no output is produced, circle the “No output” option. If the execution never ends, circle “execution never ends” and write the first four characters printed. You may assume that the general intention to break out ofcount(n)
is whenn <= 0
. You may circle multiple options as appropriate. Finally write if either there is no output or execution never ends (or both), explain why that happened in light of the recursive properties that the function did not satisfy.- (10 pts)
def count(n): print(n) count(n-1)
Base case(s) exist Progress to base case No output produced Execution never ends Output: - (10 pts)
def count(n): count(n-1) print(n)
Base case(s) exist Progress to base case No output Execution never ends Output: - (20 pts)
def count(n): if n < 0: return count(n-1) print(n)
Base case exists Progress to base case No output Execution never ends Output: - (20 pts)
def count(n): if n <= 0: print("Blast off!") return print(n) count(n)
Base case exists Progress to base case No output produced Execution never ends Output: - (20 pts)
def count(n): if n <= 0: return 0 result = n + count(n-1) print(result) return result
Base case exists Progress to base case No output Execution never ends Output: