Consider the following mystery function with one parameter
animals, a list of strings.
animals = ['cat', 'mouse', 'snake', 'chicken', 'fish']
result = mystery(animals) with the value of
x after line 5 executes?
amount after line 5 executes?
y after line 6 executes?
result by the call shown?
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]
|
def getAges(data):
orders, e.g.,:
order[index]? (in words)
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.
ok = True
for w in order[index].split()
if not w in available:
ok = False
ok shown above?
ok is True?
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:
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.
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.
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.
is_fullhouse and provide both the
code and probability of getting a full house.