Compsci 101, Fall 2012, Lab 4

There are three parts to this lab. Practice with lists and strings for the exam, APT help, and modifying/running a program that simulates poker hands.

All answers on handin pages

Part I: Exam Problem with Lists and Strings

This problem appeared on the test from Fall 2011.

Consider the following mystery function with one parameter animals, a list of strings.

def mystery(animals): # line 1 ''' animals is a list of strings ''' x = [ ] # line 2 for w in animals: # line 3 x.append(len(w)) # line 4 amount = max(x) # line 5 y = [w for w in animals if len(w) == amount] # line 6 return y[0] # line 7 Consider executing this code:
    animals = ['cat', 'mouse', 'snake', 'chicken', 'fish']
    result = mystery(animals)
Answer these questions about the code and the given value of animals:

  1. What is type of x, and what is the value of x after line 5 executes?

  2. What is the type of amount, and what is the value of amount after line 5 executes?

  3. What is the type of y and what is the value of y after line 6 executes?

  4. What value is stored in result by the call shown? What is the type of result?

  5. Rewrite lines 2-5 so that x is assigned a value via a list comprehension on one line:
       x = 
    
    

    Here's another problem from the test: Write the function getAges which has one parameter data, a nonempty list of strings in the format 'first:last:age'. The function getAges returns a list of int values based on the strings stored in data. For example:

    call returns
    getAges(['Lisa:Johnson:22', 'Xiao:Xue:34', 'Raj:Reddy:21', 'Bo:Moe:16']) [22,34,21,16]
    getAges(['A:A:8', 'B:B:3', 'C:C:17', 'D:D:42', 'E:E:20']) [8,3,17,42,20]
    getAges(['Barack:Obama:50'] [50]

  6. First write one or two sentences describing the approach you'll take in writing the function: what functions will you call, what will you loop over, what will you return?

  7. Write the function
    def getAges(data):
    
    
    


    Part II: SandwichBar APT

    Read the SandwichBar APT so you have a good understanding of the parameters to whichOrder and the return value.

    Answers on handin sheet

  8. Why is it a reasonable idea to write a one line function return 0 first, run ETester, and see what happens? Why would you expect some green on such a run?

  9. Why does it make sense (in a few words) to loop via an index over the parameter orders, e.g.,: def whichOrder(available, order): for index in range(len(order)): Instead of looping over the values in order: def whichOrder(available, order): for thing in order:

  10. Why will you need to call split on order[index] (see code below)?

    For each string/element in the list returned by split you'll need to check whether the string is in available. The code below does that, it will be nested in the loop above as shown.

         for index in range(len(order):
            ok = True
            for w in order[index].split()
                if not w in available:
                    ok = False
    
            # can sometimes return here
    
    

  11. First, what's the purpose (in words) of the boolean variable ok shown above?

  12. After the inner-loop above executes, what should be returned if the value of ok is True at the location in the code where there's a comment?

  13. The APT specifies that -1 is returned if no sandwhich can be made, when will that happen and where does the return statement go?

    Part III: 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 the Wikipedia/Poker Odds page for probabilities/odds.

    The program to calculate odds is started in Cardtester.py.

    Answers on handin page:

  14. 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 a few times and estimate what percentage of five-card poker hands contain one pair. Put that answer on the handin page.

  15. Write a function is_two_pair and implement it so that it returns true if a hand contains exactly two pairs. What's the body of the function and the probability/odds for two pairs? You'll need to modify simulate too.

  16. Write a function is_threekind 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.

  17. Write a function is_fullhouse and provide both the code and probability of getting a full house.

  18. Challenge: 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, but that's an extra challenge.