optional quiz points

Due by noon, Wednesday December 2, 2009

This is an optional assignment. If you would like to improve one or two of your quiz grades, read on.

Each program below, if done well, is worth up to one point on a past quiz. You may complete from one to four programs, to gain a maximum of two points on two quizzes. Each program must be completed on your own, and must be done well to receive the full points.

You have until Noon on December 2 to attempt these. You can turn them in anytime by emailing them to me:

$ mail knerr < myprogram.py

Cyclic Cipher Improved (not really)

The cyclic cipher (also called a ceasar cipher) you wrote back in Lab 3 is easily broken. You can either brute force it by trying all 26 shift values, or you can probably just look at the encoded output for common words, like "the", "of", "to", and so on.

One slight improvement on the cyclic cipher is to remove all spaces and punctuation, and output the encoded text in block form:

$ python ceasar.py 
 key: 5
file: /usr/local/doc/GettysburgAddress
 
ktzw xhtw jfsi xjaj sdjf wxfl ttzw kfym 
jwxg wtzl mykt wymt symn xhts ynsj syfs 
jbsf ynts htsh jnaj insq ngjw ydfs iiji 
nhfy jiyt ymju wtut xnyn tsym fyfq qrjs 
...

Write a program to perform a cyclic shift and output the encoded text in the block form shown above (4 letters, then a space, and so on with 32 letters per line). Your program should also read in the key shift value and a file name for the text to encode.

Mixed Alphabet Substitution Cipher

A slightly better (but still not great) encryption method is the mixed alphabet substitution cipher. Instead of simply shifting the alphabet, a new order of letters is created, then all letters in the original text are substituted based on this new order. For example:

          alphabet: abcdefghijklmnopqrstuvwxyz
    mixed alphabet: computerabdfghijklnqsvwxyz

with the above mixed alphabet, all a's would get translated into c's, b's to o's, c's to m's, and so on.

As stated in the link about mixed alphabet substitution ciphers, "traditionally, mixed alphabets are created by first writing out a keyword, removing repeated letters in it, then writing all the remaining letters in the alphabet". In the above example I used "computer" as the keyword.

Write a program that does a mixed-alphabet substitution cipher, asking the user for a keyword and a file to encode.

$ python mixedalpha.py 
 key: windmill
file: /usr/local/doc/GettysburgAddress
abcdefghijklmnopqrstuvwxyz
windmlabcefghjkopqrstuvxyz
 
lktq rnkq mwjd rmum jymw qrwa kktq lwsb 
mqri qkta bslk qsbk jsbc rnkj scjm jswj 
mvjw sckj nkjn mcum dcjg cimq sywj ddmd 
cnws mdsk sbmo qkok rcsc kjsb wswg ghmj 
wqmn qmws mdmp twgj kvvm wqmm jawa mdcj 
...

Caesar Cipher Decoder based on letter frequency

A caesar cipher is easy to break using known letter frequencies. The most frequently used letter in the English language is 'e'.

Write a program to read in /home/jk/inclass/ciphertext, figure out the most frequently used letter in the ciphertext, assume it must be an 'e', then calculate the shift necessary to decode the ciphertext. For example, *if* the most frequently used letter in the ciphertext file is a 'g', then it was shifted by 2 ('e' shifted by two is 'g').

Your program should ask for a file name, read in the file contents and figure out the shift, and then decode the file. What is the secret text stored in the ciphertext file?

Piet Mondrian

Piet Mondrian was a famous Dutch painter. Write a graphics program to create random images similar to Piet Mondrian's Composition paintings. Your program should create a different image each time it is run, using thick black lines and a few random red, blue, yellow, and black rectangles. Here are a few sample images from my program:

piet 1 pic

piet 2 pic

Random Circles with Quit "Button"

Write a graphics program that allows the user to create circles by clicking in the window. Each circle should be placed where the user clicked the mouse, but have a random size (not too big!) and color. The user should be allowed to create as many circles as they want. Also, to end the program, the user should click below a certain line in the graphics window. Here is an example of the program:

random circles

Dice: Rolling 5-of-a-kind

If you rolled five 6-sided dice, how often would you roll 5-of-a-kind?

Write a program to simulate rolling five 6-sided dice. Your program should ask the user how many times they would like to roll the five dice, and output how often they rolled 5-of-a-kind (both the number of times and the percentage).

If you roll the dice 1,000,000 times, how often do you get five-of-a-kind?

Recursive H's

Write a graphics program that draws an H-tree pattern as shown below.

recursive H's

Gas Molecules

Write a graphics program that animates 10 small circles. Each circle should start in a random location with a random direction. When a circle "hits" a wall or edge of the graphics window, it should bounce off and start heading in the opposite direction. Here is a short movie from this animation: