Outline for October 19, 2023
Reading:
§4.5
Due:
Homework 2, due October 26, 2023
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
]
Greatest common divisor [
gcd.py
]
Nested lists: is an item in a list? [
isinlist.py
]
Tower of Hanoi [
hanoi.py
]
Using random numbers
Problem: compute
π
by tossing darts at a unit square
First build routine to simulate dart toss at unit square [
mc-1.py
]
Then build routine to see if co-ordinates are in unit circle [
mc-2.py
]
Then build routine to read in number of tosses [
mc-3.py
]
Put it all together [
mc-4.py
]
Graphics! [
mc-5.py
]
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
[email protected]
ECS 235A, Computer and Information Security
Version of October 18, 2023 at 10:05PM
You can also obtain a PDF version of this.