WARMUP
Minesweeper is a single player game where players reveal spots on an M x N grid, with Z mines. If they reveal a mine, they lose. When they reveal a non-mine, the tile is replaced with the number of adjacent mines, or, if there are no adjacent mines, all adjacent squares are revealed.
They may mark un-revealed spots as mines. They may then ask a tile to assume those spots are correct, and (if the number of adjacent mines is satisfied) reveal all other un-revealed adject tiles.
Describe the necessary parts of a program to play Minesweeper.
HW Review
What did people have the most trouble figuring out? ask random
Programming
Thing big: what was this homework about? ask random
Key Research Ideas
- sequential vs conditional loops
Sequential Iteration with for loops
A for block iterates over the items in a collection, and executes for each of those items. Here are several examples:
__author__ = 'cap10'
tot = 0
for i in range(1, 10):
tot += i
print(tot)
for s in ["some", "different", "words"]:
print(s)
print(s)
first, last = 0, 1
print(last)
for i in range(0,20):
temp = last
last += first
print(last,last/temp)
first = tempWhat does the last iteration correspond to?
Conditional Iteration with while
A while block loops as long as a condition is met. It can simulate a for loop:
__author__ = 'cap10'
sumFor, sumWhile = 0, 0
start, end = 0, 10
for i in range(start, end):
sumFor += i
while start < end:
sumWhile += start
start += 1
assert sumFor == sumWhilebut more generally it is used for checking a more complicated conditional. If we wanted to calculate the Fibonacci sequence to a fixed approximation of $\phi$ rather than for a fixed number of steps:
__author__ = 'cap10'
def findFib(err):
first, last = 1, 2
phiEst = 1.0 # 1/1
while (phiEst - last/first)**2 > err**2:
phiEst = last/first
temp = last
last += first
first = temp
return (first, last, last/first)
if __name__ == "__main__":
print(findFib(1e-6))
print(findFib(1e-12))Advanced Version
We use discussed these two loops constructs, for loop for the mid-point method and while loop for bisection root finding.
Now your task is to implement another integrator (the Trapezoid Method in trapeziod.py) and another root finder (the Secant Method in secant.py).
Once you’ve done so, the following code (already in your homework directory) should work:
$ ./try_integrators2.py
...
$ ./try_roots2.py
...
HOMEWORK
In the file fun_plots.py, you will find several functions.
Implement your own plotting function in calculus_plotter.py that plots a function, it’s derivative, and it’s integral on the same graph. Use different colors or line types to distinguish the curves and include a legend.
Finally, edit make_fun_plots.py to use your plotting function to make plots for each of the functions in fun_plots.py.
