Compsci 101, Fall 2017, Lab 9

The lab

In this lab you will:

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.

  1. What does get_deck return?
  2. How does get_hand simulate dealing one hand from a deck?
  3. What is the purpose of variable pc in function simulate?
  4. 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).
  5. 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.
  6. 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.
  7. 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.
  8. 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.

  1. 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.
  2. If "dazed" shifts to "spots" with a shift of 15, then what shift is needed to shift "spots" to "dazed"? Why?
  3. 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



After writing the code you should be able to answer these questions on the online form.

  1. How many key-word-pair combinations are there?
  2. 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.