More Recursion
Tom Kelliher, CS18
Feb. 26, 1996
Computing recursively
Implement this equation
Implement this equation
- Is one method ``better'' than the other?
- Define ``better''
- Significance of the difference?
You have n friends, but only enough time to visit k of them
How many different sets of friends can you visit?
Consider 1 friend:
- Included in some sets: n - 1 remaining friends, with k - 1 to visit
- Excluded in some sets: n - 1 remaining friends, with k to visit
- Base cases?
- m friends, m to visit
- m friends, 0 to visit
The equation:
Write the function
A certain robot can take steps of length 1 or 2.
Count the number of different ways in which it can take a walk of n steps.
- How many length 1 walks?
- Length 2?
- Length 3?
- ...
- Generalize to n in terms of shorter walks
-
Write a recursive function to convert a character string of n digits
to the corresponding integer number. For example, if the input string is
"5274", return the number 5274. Observe that int('5') -
int('0') is the number 5. The following demonstrates the idea:
- 5274 = (527)10 + 4
- 527 = (52)10 + 7
- 52 = (5)10 + 2
-
To convert a non-negative decimal integer num to a binary number,
repeatedly perform integer division by 2 of successive quotients. The
remainders, written in reverse order, are num expressed in binary.
Write a recursive function to print a non-negative decimal integer in
binary.
Thomas P. Kelliher
Fri Feb 23 11:44:06 EST 1996
Tom Kelliher