Compsci 101, Fall 2017, Lab 9
The lab
In this lab you will:
- calculate the odds of poker hands such as three of a kind
- Find pairs of words for which one is the encryption of the other
using a Caesar Cipher.
There is code to snarf for lab 9, that
code is also here.
Getting Credit for Lab 9
To get credit for this lab, you will need to do the following by Sunday
night.
Part 1A: Take the survey on Sakai
There is a link to a survey on Sakai for you to give us feedback on how
things are going for you, the labs, the consulting hours, etc.
Please fill out the survey in lab on Nov 8-9, 2017.
Part 1: Poker Odds
In this part of the lab, we'll be estimating the odds of various hands
of poker, specifically a version called 5 card stud, using a Monte Carlo
method. The idea is to generate many, many random poker hands, and count
which fraction of them are two pairs, full houses, straight flushes,
which fraction are four of a kind, etc.
See poker
hands for types of hands, and
Wikipedia/Poker
Odds page for probabilities/odds.
For example, here is a full house, 3 of one kind and two of another
kind: three jacks and two fours.
The program to calculate odds is started in
CardTester.py
Put your answers in the online form.
Most of the code provided is for dealing cards, managing hands, and
printing the hands. You should look carefully at both
get_rank_counts
, is_pair
, and
simulate
to see how the provided code determines how many
hands contain one pair. Run the program twice now!
Answer the following questions.
-
What does get_deck return?
-
How does get_hand simulate dealing one hand from a deck?
-
What is the purpose of variable pc in function simulate?
-
Run the program twice and observe statistics about how many five-card poker
hands contain one pair. Enter the number of hands that contain one pair
when 20,000 hands are dealt (in the online form).
-
Write a function two_pairs so that it returns true if a hand contains
exactly two pairs, and false otherwise. Model the function on one_pair.
Modify main by adding a call to report statistics based on dealing 20,000
hands and reporting how many are two pairs. Then answer the questions on
the form: how many hands out of 20,000 have exactly two pairs? Paste your
two_pair function in the form too.
-
Write a function three_kind to determine when a hand has three of a kind
exactly (not four of a kind and not a full house which is three of one rank
and two of another rank). For example, [J,J,J,Q,3] is three of a kind, but
[3,3,3,8,8] is not because it's a full house. Then answer the questions on
the form: how many hands out of 20,000 have exactly three of a kind? Paste your
three_kind function in the form too.
-
Write a function full_house to return true when a hand is a full house, and
false otherwise. Then answer the questions on the form: how many hands out
of 20,000 have a full house (three of one kind, two of another)? Paste your
full_house function in the form too.
-
In poker a flush is all cards of the same suit, e.g., all
spades, all hearts, etc. Write code to determine if a poker hand is a
flush, simulate to find out how many hands are flushes. This might be an
overestimate because a straight flush is better than a flush.
Enter the number of hands out 20,000 that are
flushes. Paste your code too. (hint: put the suits in a set)
On your own you can try four of a kind, but we won't go over that.
Part 2: Solve An Encryption Problem
In a previous assignment you completed code to do a Caesar Cipher. A
simplified version of a program that encrypts using a Caesar Cipher is
given in SimpleCaesar.py, you can snarf that. It's simplified because it
only works with lowercase strings that contain letters 'a' - 'z' and no
other characters.
If you encrypt "tear" with a shift of 22 you get "pawn". If you encrypt
"layout" with a shift of 20 you get "fusion".
Write a Python program that
finds every word in the file lowerwords.txt (there are 45,356 words) that
can be shifted by some shift between 1-25 to find a string that's a real
word, such as the examples above with "tear" and "layout". Your program
should print all the words in the format shift, word, encrypted, e.g.,
something like what's shown just below.
1 ad be
1 add bee
1 adder beefs
...
15 dated spits
15 daze spot
15 dazed spots
...
22 ex at
22 ferns banjo
Your program should also print the total number of key-word pair
combinations (each line above is such a key-word pair combination) where
the first word encrypts to the second with a Caesar shift. Note that
("dazed", "spots") and ("spots", "dazed") are two different pairs and have
different keys with them.
Answer questions about the program on the online lab form
before you write code.
-
Give one reason you think it makes sense to write a function to read the
file lowerwords.txt and return a list of all the words in the file.
-
If "dazed" shifts to "spots" with a shift of 15, then what shift is needed
to shift "spots" to "dazed"? Why?
-
Before you write and run the program, how long do you think it will take to
run to find all word pairs?
Part 3 - Coding it
You should not modify SimpleCaesar.py. Instead you should create a new
Python module, call it CaeserDiscover.py. In this code you can include
import SimpleCaesar
Then in any function in the program you can call SimpleCaesar.encrypt(x,y)
since the code is accessible via the import. Here are some ideas to help
you get started
-
Read the file lowerwords.txt into a list (or a set) using code and
functions you've seen before in assignments, on the midterm, and that you
can find online
-
When searching to see if a string is in the dictionary of words, use a set
of all the words, not a list. Sets are really, really fast for look-up
using if word in collection: (make collection a set)
-
Loop over every possible shift 1-25 using something like for shift in
range(1,26):
After writing the code you should be able to answer these questions on the
online form.
-
How many key-word-pair combinations are there?
-
Describe what happens if you use a list of all the words instead of a set
when searching. Try it...
After submitting the lab form, then submit the code you wrote for both
parts of the lab with ambient. Use lab09 as the submission folder.