The purpose of this lab is to:
Before you begin, please create a folder called lab03 inside your cs150 folder (for a refresher, here's how you did this on lab 02). This is where you should put all files made for this lab.
pyramid.py: 12 points, partner allowed.
If you choose to work with a partner, only one of you should submit a solution, but both of you should indicate your partner and who submitted a solution in your README files. You should also both read the Recurse Center's guide on pair programming before you begin.
Write a program called pyramid.py that draws a pyramid of bricks based on user input.
Input: An integer for the width of the image (width) and the height in bricks of the pyramid (n).
Output: An image of a pyramid that is n bricks tall in a square canvas width wide (and thus width tall).
Here are three sample outputs for your reference. Notice that the pyramid doesn't necessarily fill the entire canvas to the right and to the top; if the canvas width is not evenly divisible by the number of bricks, then there will be extra blank space. (A question for you to ponder: why is there so much blank space in the third example? Seems like you could fit lots of extra bricks both to the right and up top...)400 x 400, 3 bricks.
Write pseudocode to draw the appropriate pyramid. The algorithm is
Now that you have a detailed algorithm in pseudocode, translate it (bit by bit!) into a Python program named pyramid.py. Although your final program should get the width and number of bricks from the user, you may want to temporarily hard-code this values into your program (using the example values above, perhaps) for now because it will make testing easier for now.
We have provided you with a module picture that lets you draw pictures. To use it you need to:
canvas = picture.Picture(600, 800)This creates a new picture object called canvas which you'll be able to draw upon and display. The parameters are a pair of integers that specify the width and height of the canvas. Naturally, you can use other values if needed.
canvas.setOutlineColor(r, g, b)
Here r is an expression indicating how much red the pen color should have (from 0 to 255). Similarly, g and b indicate how much green and blue are in the pen color. Some useful (r,g,b) values: (255, 255, 0) is yellow, (0, 0, 0) is black, and (0, 255, 255) is cyan.
The function to set the fill color is what you might expect:
canvas.setFillColor(r, g, b)
You'll also want to use the drawRect and drawRectFill function to draw rectangles and filled rectangles. For example,
canvas.drawRectFill(x, y, w, h)
would draw a filled rectangle in the current pen color. The rectangle would be w pixels wide and h pixels tall. The upper left corner of the rectangle would be located at position (x,y). (Recall that the picture coordinate system, (0,0) is the upper left pixel.)
As you may recall from class, the picture window will close as soon as your program ends, so if you want to be able to admire your creation, you'll need to force the program to wait. The easiest way to do this is by ending your program with an input statement:
Now when you run your program, it will open the picture window and keep it open until you press enter.
Try running the program with the examples given above as well as some others. Make sure you have gaps where you ought to, and that there aren't gaps where there shouldn't be gaps! Your pyramid should not be sloping to one side or floating in the middle. You shouldn't have some bricks that are larger than others. If it looks fishy, go back and examine your math equations, checking that the "integer division" is being used appropriately.
Don't forget to let the user input the width and number of bricks, if you were testing the program with hard-coded values.
Please handin your lab up to this point so that we have some portion of your code submitted.
primes.py: 12 points, individual.
As you may know, a number x is said to be prime if x is at least 2, and the only proper factors of x are itself and 1. So the first few primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, etc. 4 isn't prime, since it is divisible by 2. Same goes for 6 and 8. 9 is out thanks to 3. And so on. There are a lot of primes. More precisely, there are infinitely many primes. This can actually be shown pretty easily; ask if you're curious.
A twin prime is a pair of prime numbers that differ by exactly 2. So (3,5), (5,7), (11,13), (17,19) and (29, 31) are all twin primes. Note that not every prime is part of a twin prime. It is conjectured that there are infinitely many twin primes too, but no one knows for sure.
Write a program called primes.py that prints out some number of primes and the number of twin primes amongst them.
Input: A number n.
Output: The first n primes, and the number of twin primes amongst these n.
If the user enters 13 then the output should be
The first 13 primes are: 2 3 5 7 11 13 17 19 23 29 31 37 41 Amongst these there are 5 twin primes.
Note that (41, 43) is a twin prime, but we didn't count it since 43 wasn't amongst the first 13 primes.
Write pseudocode to solve this problem. You should decompose your main algorithm into small manageable chunks. For example, you should
You may want to use a while loop as you search for primes, since you won't know ahead of time just how far you need to go. Ask if you're not sure what a while loop is, or Google "python while loop".
Try your program with a variety of inputs n. Certainly you should try n=0,1,13 but you should also try n=14 to get that one extra twin prime, as well as others!
Please handin what you've completed thus far.
master.py: 14 points, individual.
Mastermind is a neat (although oftentimes frustrating) puzzle game. It works a something like this: There are two players. One player is the codemaker (your porgram), the other is the codebreaker (the user). The codemaker chooses a sequence of four colored pegs, out of a possible six colors (red, blue, green, yellow, orange, and purple). He may repeat colors and place them in any order he wishes. This sequence is hidden from the codebreaker. The codebreaker has 10 chances to guess the sequence. The codebreaker places colored pegs down to indicate each of her guesses. After each guess, the codemaker is required to reveal certain information about how close the guess was to the actual hidden sequence.
In this part of the lab, you will create a program to play Mastermind, where computer is playing the codemaker, and the human user is the codebreaker. Thus your program needs to generate a secret code, and repeatedly prompt the user for guesses. For each guess, your program needs to give appropriate feedback (more detail below). The game ends when either the user guesses correctly (wins) or uses up 10 guesses (loses).
The trickiest part of this game is determining how to provide feedback on the codebreaker's guesses. In particular, next to each guess that the codebreaker makes, the codemaker places up to four clue pegs. Each clue peg is either black or white. Each black peg indicates a correct color in a correct spot. Each white peg indicates a correct color in an incorrect spot. No indication is given as to which clue corresponds to which guess.
For example, suppose that the code is RYGY (red yellow green yellow). Then the guess GRGY (green red green yellow) would cause the codemaker to put down 2 black pegs (since guesses 3 and 4 were correct) and 1 white peg (since the red guess was correct, but out of place). Note that no peg was given for guess 1 even though there was a green in the code; this is because that green had already been "counted" (a black peg had been given for that one).
As another example, again using RYGY as our code, the guess YBBB would generate 1 white peg and 0 black; yellow appears twice in the code, but the guess only contains one yellow peg. Likewise, for the guess BRRR, only 1 white peg is given; there is an R in the code, but only one. Below is a table with guesses and the correponding number of black and white pegs given for that guess (still assuming the code is RYGY).
|guess||black pegs||white pegs|
Check here for an online graphical version of the game (where their red pegs are our black pegs).
A sample run of our text-based program may look like this:
%python3 master.py I have a 4 letter code, made from 6 colours. The colours are R, G, B, Y, P, or O. Your guess: GGGG Not quite. You get 0 black pegs, 0 white pegs. Your guess: YYYY Not quite. You get 1 black pegs, 0 white pegs. Your guess: YOYO Not quite. You get 0 black pegs, 2 white pegs. Your guess: PPYO Not quite. You get 1 black pegs, 2 white pegs. Your guess: POYB Not quite. You get 1 black pegs, 3 white pegs. Your guess: PBOY You win! So clever.
Once you understand how the game works, you should design a pseudocode plan of attack. The general steps are:
Now that you have some of the kinks worked out in theory, it is time to write your program master.py.
You may assume the user always provides a guess with the available colors, and always in uppercase.
Make and use an integer constant NUM_TURNS that represents the number of allowable turns (say, 10).
To generate the code, write a method generateCode() that generates the codemaker's code (and returns it as a String to the caller). That is, this method should randomly generate 4 colored pegs, selected from R, B, G, Y, O, and P, and return it as a 4-letter string. You'll want to use the random methods as discussed in lab02 in order to randomly generate a color for each peg. In particular, you'll generate an integer between 0 and 5 inclusive, and use if-statements to map each result to one of the 6 colours. Test your generateCode() method thoroughly before continuing. No, seriously, test it before continuing.
Next, write a method clue(code, guess) that prints out the white and black clue pegs according to the given guess and code, and returns true if code equals guess, and false otherwise. Translate the pseudocode above to help you out.
Note that you can "change" the i-th character in a string s to an 'x' as follows:
s = s[0:i] + "x" + s[i+1:len(s)]
Also note you can omit the len(s) from the above expression. That is, if you write s[i:], Python interprets that as the substring of s from position i to the end. Similarly, s[:i] denotes the substring of s from the beginning up to (but not including) i.
It is hard to test your program when you are given a random code that you don't know. Therefore, you should either hard-code in a code for testing purposes (for example, the code that you checked by hand on the prelab), or you should allow the user of the program the option to input a code (this would make our graders very happy, so I like this option.)
README: 2 points, individual.
As with every lab, your last job prior to submission is to complete a brief write-up in a README file. If you haven't already done so, please create a new README file in your lab02 folder.
In this file, write a sentence or two about what you learned in this lab. Also give an estimate of the amount of time you spent on the lab. If you have further thoughts about the lab (e.g. parts that were confusing, helpful, annoying, fun, or challenging), please let us know.
Again, if you worked with a partner on Part 1, your README file should indicate the name of the partner, and specify which of you submitted the solution to that problem.
If you followed the Honor Code in this assignment, insert a paragraph attesting to the fact within one of your README file.
I affirm that I have adhered to the Honor Code in this assignment.
You now just need to electronically handin all your files. As a reminder
% cd # changes to your home directory % cd cs150 # goes to your cs150 folder % handin # starts the handin program # class is 150 # assignment is 3 # file/directory is lab03 % lshand # should show that you've handed in something
You can also specify the options to handin from the command line
% cd ~/cs150 # goes to your cs150 folder % handin -c 150 -a 3 lab03
You should have submitted the following files:
pyramid.py (unless you worked with a partner, and your partner submitted) primes.py master.py picture.py (you shouldn't have touched this, but it makes grading easier) README