15-112 Spring 2012
Homework 9
(Monte Carlo + Tetris)
Due Sunday, 1-Apr, at
10pmRead these instructions first!
- This hw is SOLO.
- Create a directory named hw9, and place every required file (as
specified below) in that directory. To submit, zip that directory into the
file hw9.zip, then submit just that one zip file.
- If you do not know how to zip a directory, contact a CA at office hours
for help (ideally, more than a few minutes before the due date/time).
- Be sure to zip the directory. Do not create a rar file, or a tar
file, or any other kind of archive. Just zip. Again, if unsure, contact a
CA at office hours for help.
- There are no starter files or public or private autograders this week.
- Grading for this homework may be extrapolated based on randomly-selected
portions of your submissions. However, if we notice any missing portions,
these will of course be deducted in any case.
- Up to 10 points in style points may be deducted from your score on this
hw. So use good style!!!
- Monte Carlo [10 pts]
Solve each of the following problems using Monte Carlo techniques (even if
you can solve them exactly with math, you must use Monte Carlo techniques here).
For each problem, write a program that, when run, simply prints out the answer
as a number between 0 and 1. Assume a large enough number of trials to get
a reasonably accurate answer while still running in a reasonable amount of time
(say, less than a few seconds per program).
- Random Coprimes [5 pts]
Say that you pick two numbers at random between 2 and 1 billion, inclusive.
In the file randomCoprimes.py, write a program that uses Monte Carlo techniques
to compute and print the probability (as a number between 0 and 1) that these
two numbers are coprime (that is, that they have no common factors
besides 1).
- Sticky Triangle [5 pts]
Say that you take a stick and randomly break it in two, then take the longer
of the two pieces and randomly break that piece in two, so now you have three
pieces. In the file stickyTriangle.py, write a program that uses Monte
Carlo techniques to compute and print the probability (as a number between 0 and
1) that the three pieces can form a triangle.
- Tetris [90 pts]
In the file tetris.py, write Tetris
exactly according to the design given in
this step-by-step tutorial. You may not use a different design, even if you
think there's a better way to do it (there probably is, but you still have to do
it this way).
- Tetris Bonus (optional) [up to 10 pts]
For bonus, you may
do some of the additional suggestions on the last page of the tutorial. Submit
your bonus in the file bonus-tetris.py, and list all your bonus-worthy changes
in a comment at the head of the file. Note that your tetris.py submission
must match the non-bonus spec, and any and all bonus must be exclusively in your
bonus-tetris.py file.
carpe diem - carpe
diem - carpe diem - carpe diem - carpe diem - carpe diem -
carpe diem - carpe diem - carpe diem