Outline for February 12, 2018
Comments on homework: on the whole, quite good!
Make your program as simple as possible
Comments should
not
repeat the code, but explain at a higher level what is being done and why
Put a comment at the head of each function saying what the function does, what (if anything) it returns, and what its parameters mean
Indent your comments to be in line with the code to which it refers
Recursion
n
factorial [
nfact.py
]
Thinking recursively [
recfun.py
]
First: think of the recursive case (write the problem in terms of something involving a smaller instance of the problem)
Next: think of base case (when to stop)
Example: Find the length of a string
Example: Does the string only have alphabetic characters in it?
Example: Find the maximum element of a list
Example: Construct a string from a list of strings
Example: Reverse a string
Recursion
Palindromes [
palindrome.py
]
Fibonacci numbers [
rfib.py
]
Sum of digits [
sumdigits.py
]
isinstance(
obj
,
type
)
function
type
is
bool
,
float
,
int
,
list
,
str
,
tuple
Nested lists: is an item in a list? [
isinlist.py
]
Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: Version of February 11, 2018 at 6:30PM
Winter Quarter 2018
You can get a PDF version of this