Run update21 to create the cs21/labs/10 directory containing the starting files for this lab Then cd into your cs21/labs/10 directory and create the python programs for lab 10 in this directory (handin21 looks for your lab 10 assignments in your cs21/labs/10 directory).
$ update21 $ cd cs21/labs/10
In a file named towersofhanoi.py you will implement two versions of a function (one recursive, one iterative) that takes the number of disks and returns the minimum number of steps it takes to solve a Towers of Hanoi puzzle for the given number of disks. Both functions solve the same problem, but one uses an iterative algorithm while the other uses recursion. As a reminder, you are not solving the actual puzzle, only counting the steps needed to solve the puzzle.
Your main program should:
The number of steps to solve a puzzle with N disks is defined by the following recursive definition:
for 1 disk it takes 1 step for 2 disks it takes 1 + 2 times the number of steps to solve for 1 disk = 3 for 3 disks it takes 1 + 2 times the number of steps to solve for 2 disks = 7 for 4 disks it takes 1 + 2 times the number of steps to solve for 3 disks = 15 ...Here are some sample runs of a working program:
$ python towersofhanoi.py This program computes the minimum number of steps it takes to solve a Towers of Hanoi puzzle of a given number of disks. Enter the number of disks in the puzzle: 3 For a Towers of Hanoi puzzle of size 3 it takes: 7 steps (iteratively) 7 steps (recursively) $ python towersofhanoi.py This program computes the minimum number of steps it takes to solve a Towers of Hanoi puzzle of a given number of disks. Enter the number of disks in the puzzle: 5 For a Towers of Hanoi puzzle of size 5 it takes: 31 steps (iteratively) 31 steps (recursively) $ python towersofhanoi.py This program computes the minimum number of steps it takes to solve a Towers of Hanoi puzzle of a given number of disks. Enter the number of disks in the puzzle: 10 For a Towers of Hanoi puzzle of size 10 it takes: 1023 steps (iteratively) 1023 steps (recursively)
$ python palindrome.py Enter a phrase: kayak 'kayak' is a palindrome $ python palindrome.py Enter a phrase: puppy 'puppy' is not a palindrome $ python palindrome.py Enter a phrase: madam, I'm Adam 'madam, I'm Adam' is a palindrome
print insertPattern("puppies", "*") p*u*p*p*i*e*s print insertPattern("pumpkin pie", "-") p-u-m-p-k-i-n- -p-i-e print insertPattern("yolo", "at") yatoatlatoYour function should have the same behavior as pattern.join(list(phrase)), but you should not use join() or list() in your recursive solution
print isSorted([1, 2, 4, 5 , 8]) print isSorted([1, 12, 4, 5 , 8]) print isSorted(["apple", "cranberry", "pumpkin", "turkey"]) empty = [] print isSorted(empty) output: True False True True
In triangle.py, write a recursive function:
fracTriangle(window, top, left, right, color, n)that takes as parameters a GraphWin window, three Points (top, left, and right), a color that will be "black" or "white", and an integer n which is the recursive depth. The fracTriangle function should draw a triangle of color color in the graphics window and then, depending on the depth n, either return or recursively use the fracTriangle function to draw smaller triangles as described below.
Recall that every recursive function needs a base case where the
recursion ends. For your fracTriangle function, the base
case should occur when n is zero; your fracTriangle
function should use top, left, and right to draw
a single triangle of the given color, and then return.
For example, with color="white" and n=0, your
function should produce something like the single triangle below.
Your image should not contain the text labels "top", "left", and "right".
We just put them in the image to clarify how the triangle is drawn.
If n is greater than zero, your function should use top, left, and right to draw a triangle of the appropriate color (same as before) and then use three recursive calls (with depth n-1) to draw smaller triangles inside the given triangle. The three recursive calls should draw triangles of the opposite color defined by the points illustrated below:
Note: given two points (graphics Point objects!), like top and right, you can calculate and create a new midpoint like this:
midx = (top.getX() + right.getX()) * 0.5 midy = (top.getY() + right.getY()) * 0.5 midright = Point(midx, midy)You could even write a function (hint, hint!) to return a midpoint, given two starting points. :)
For reference, the diagram below was produced with depth n=1 and
color="white" (again, your program shouldn't label the points).
Note how the center area (marked here in French) is white from the
first call to fracTriangle, and the three black triangles
(from the 3 recursive calls to fracTriangle) are drawn on
top of the original white triangle.
Here are a few more examples.
The left image below was drawn with n = 2 and
color = "white", and the right image was drawn with
n = 3 and color = "white":
Using larger depths can lead to attractive patterns, but can take a long
time! The following diagram was drawn with color="white" and
n=7:
In main(), your program should read in a recursive depth n and initial color ("black" or "white") and draw the pattern of the appropriate recursive depth.
To summarize, here are some hints for the recursion:
Once you are satisfied with your programs you can hand them in by typing handin21 in a terminal window.