|CS 220 01/ Zelle||Winter 2018|
Chapter 1 Review Questions (pt 1) (due 1/8, class time)
Do T/F questions 1--5 and Multiple Choice 1--3 from pp 33,34. Write answers on paper suitable for hand-in at the start of class.
statistics functions (due 1/10, class time)
Write the Python functions in stats.py as specified on page 15. You can get the initial code from the "textbook resources" section of our homepage. Be prepared to share your solutions with the class. If you are doing this on your computer, it would be good to test your code :-).
Blackjack Function Specifications (due 1/12, class time)
Write specifications for the 6 helper functions of the Blackjack simulation that we discussed at the very end of class today. I think the 6 helpers are: printIntro, simNHands, printReport, simHand, dealCard, and handTotal. Your specifications should be in the same form as the stats.py (page 15).
The starting point is to take the structure chart that I drew on the board and figure out information flow. That is, determine what parameters have to be passed in order for each function to do its job. Then write the function headings with parameter names. Finally, provide approriate pre and post conditions that state what the function does. NOTE: You should not write the code (implementation) for these functions, just the specifications (function headings with pre and post conditions). Try to make your specifications as precise, concise, and complete as you can. You should be able to hand them off to another class member to (potentially) implement.
Blackjack Simulation (due 1/17, midnight)
OK, now you are supposed to write the code :-). The design we have so far is in blackjack.py. You should try to be faithful to it. Make sure to add appropriate pre and post conditions where they are currently lacking. Hand it in on Socrates.
Do T/F questions 6--10 and Multiple Choice 4--10 from pp 33,34. Write answers on paper suitable for hand-in at the start of class.
Finish up the selection sort algorithm that we discussed in class today by completing the posOfMin(lst, i) function. Your code should include appropriate pre- and postconditions. You should test your selsort to make sure it works (you may want to include a main fuction for testing, but you could also just use the shell window). Turn your file in on Socrates (by midnight) and come to class with a theta analysis of the selection sort algorithm.
Write a Complex class to implement the normal arithmetic operations of addition, subtraction, multiplication, and division on complex numbers. Your class should implement the standard Python "rib" methods __add__, __sub__, __mul__, and __truediv__. (The last is the Python 3 version of __div__.)
You should also hand in a test_complex.py file that implements unit test for your class with the unittest Python module. Look at the test_rational.py example in the book for the basic idea. I suggest you develop your Complex class and TestComplex class hand-in-hand. Write a test or two for the function that you are implementing, then do the implementation and make sure that it passes the tests. Here are a few operation results that may be usefule for testing:
let c1 = 2+3i and c2 = 4-5i c1 + c2 = (6-2i) c1 - c2 = (-2 + 8i) c1(c2) = (23+2i) c1/c2 = (-0.17073170731707318+0.5365853658536587i) c1/c1 = (1+0i)
Write a program (mandel.py) that uses your Complex numer class to compute and display a black and white image of the Mandelbrot set. As discussed in class, the heart of the program is a function to count the number of iterations of f_c(Z) = Z*Z + c that happen until |Z| > 2 or some max number of iterations is reached. The basic algorithm from class is:
def countIters(c, max_iterations): z = Complex() # complex 0, by default count = 0 while count < max_iterations and z.mag2() < 4: z = z*z + c count = count + 1 return count
Notes: You will have to extend your Complex class with the mag2 method. This returns the square of the magnitude of the number, which is given by real*real + imag*imag. This program will be SLOW! start with a realtively small window size until you get it working. As discussed in class, you should NOT do setCoords on the graphics window. Leave the window in it's natural coordinates and convert pixel locations into the mandelbrot coordinates x: -2.5 to 1 and y: -1 to 1. Then use win.plotPixel(px,py,color) to set the color of a pixel. Points that reach max_iteration (say, 1000) are colored black, other points are white. It should look like this:
Additional tips on efficiency: Redrawing the picture each time a new pixel is plotted will really slow down your pogram. You should turn off the automatic updating by setting autoflush to False when you create the graphics window: GraphWin("Mandelbrot", 350, 200, autoflush=False) Then your "picture" will not show up in the window until all the pixels are drawn. Of course that makes it hard to see whether the program is working. As a compromise, you can strategically place a win.update() command in your program after each line (or column) of pixels has been set (after the inner loop).
Read through section 3.5 and answer review questions T/F 1--10 and Multiple Choice 1--7.
ps: I'm delaying the GridSample project until we have a little more time to discuss it in class.
The pypy version in the Linux Lab implements Python 2.7. To make your programs compatible, put this line at the very top:
from __future__ import division, print_functionIf your program does some textual input in the shell, then you will also need to redefine input something like this.
try: input = raw_input except NameError: pass
For those of you wishing to install pypy on a Windows computer, you need to download the appropriate windows binary file from here: www.pypy.org. Get the one that is Python 2.7 compatible. Once downloaded you can unzip the folder and place it anywhere that is convenient on your system. I will show in you class how to make a shortcut in your class folder to start up IDLE with pypy.
For Mac users the simplest way to install pypy is with the Homebrew package mananger. Complete instructions are here: macappstore.org.pypy. You can then run pypy from a terminal window. If you want to use IDLE, you can type this command: pypy -m idlelib.idle.
Complete the GridSample class so that it passes the unit tests. You can get the starter code here.
Do short answer questions 3 and 5 on pages 102 and 103.
Complete the mandel2.py program that uses GridSample to compute and draw a color Mandelbrot image.
I've posted the lab solution cds.py as well as a practice exam PDF in our handouts folder.
Modify your GridSample project to incorporate iterators (via generators) in two places. First, change locations() so that it does not make use of an explicit list, but rather, produces the sequence of pixel pairs (px,py) using a generator. You can then remove the creation of the locations list from the constructor of the GridSample class. You may also have to change any internal uses of this old list into calls to self.locations(). Functionally, your class should be unchanged, so you can test it by running the mandel2.py program that produces the colored Mandelbrot set. I will post a mandel2_solution.py in handouts, in case yours is not yet working.
The second change is to implement an iterator for the GridSample that traverses the locations in the grid as a sequence of cells. A cell has attributes for location, coordinates, and value. Add this cell class to your module:
class Cell(object): def __init__(self, loc, coords, val): self.location = loc self.coordinates = coords self.value = valYou can test this out using the mandel3.py program, which has a modified version of displayGrid that uses iterations through cells.
Read Chapter 5 and answer the True/False and Multiple Choice questions from pages 181 and 182.
Write a Mandelbrot exploring program with a GUI interface. Your program should make use of our existing classes Complex, GridSample and Stack. New classes MandelAppModel, MandelAppView, and MandelApp will help organize the complete application (using the MVC design pattern).
The detailed design is up to you, but your application must provide the following abilities:
The MandelAppModel will take care of the maintaining the "state" of your program. It will have a GridSample and a Stack as instance variables to maintain the current grid and coordinates of previoius grids. The top entry on the stack will be the coordinates of the current grid. The MandelView will have a graphics window and some interface widget(s). For example, it may have a text widget that displays status messages to the user. The MandelApp will be the main application class (the controller). It will contain an instance of MandelAppView and MandelAppModel. It should have a run method that implements the event loop for the program.
Important! You have two weeks to complete this project. That's because it will take some time to figure it out and get all the parts working together. Start early and approach it systematically. Start by writing a program that is organized as described, but just displays the Mandelbrot set. Then incrementally add features. Make sure that you plan for being able to vary the size of the app so that you can start with small windows for quick testing and debugging. You may use my gridsample.py if you are not confident that yours is working.
Implement a Queue class as a linked list of Nodes. It would also be a good idea to write unit tests for your queue to make sure that it works. I will test your Queue with my own tests, so you do not need to hand in the tests, just the file with the Queue and Node classes.
Write a program to draw the "arrow" fractal. The generator is a "hump" created by tracing the "outline" of two side-by-side equilateral triangles. This generator is alternated similar to the dragon curve, except the first bump is "in" rather than "out". Here are pictures of the level 1, 2 and 3 curves:
Do the T/F and multiple choice questions at the end of Chapter 6. Have them ready for hand-in at the beginning of class.
Use the DTree class that we discussed in class to implement the Animal Game program (Exercise 10, page 251).
You will have to store your decision tree in a file to save the data between runs of the program. The simplest way to do this is to "serialize" the DTree object using Python's pickle module (really). Pickling allows an arbitrary object to be "dumped" into a file and then loaded again later. Here's an example of writing an object to a file:
import pickle ... # open a file for writing. # The "b" in the mode indicates a binary (non-text) file. saveFile = open("pickleTest", "wb") # dump some object to the file pickle.dump(someObject, saveFile) saveFile.close()And here's how to load it back in again:
infile = open("pickleTest", "rb") someObject = pickle.load(infile) infile.close()
On start up, your program should try to load the current information from the file "animal.db". If this file does not exist or the contents are unreadable (you get an IOException), the program should print a warning that no existing database is available and start a new decision tree "from scratch". That means creating a tree with a single node for some default animal, such as "dog." When the program quits, the current tree should be saved back to animal.db. Note, this means you can always "rebuild" the game by simply deleting (or renaming) the database file.
You will hand in both your source code and an example animal.db file. Your source code should be fully documented, including pre and post conditions for each public method of DTree.
Groups are OK. Four is the maximum number of bodies in a group.