Consider the following mystery function with one parameter
animals
, a list of strings.
animals = ['cat', 'mouse', 'snake', 'chicken', 'fish'] result = mystery(animals)Answer these questions about the code and the given value of
animals
:
x
, and what is the value of x
after line 5 executes?
amount
, and what is the value of amount
after line 5 executes?
y
and what is the value of y
after line 6 executes?
result
by the call shown?
What is the type of result
?
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):
whichOrder
and the return value.
return 0
first, run ETester, and see what happens?
Why would you expect some green on such a run?
orders
, e.g.,:
order
:
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
ok
shown above?
ok
is True at the location in the code where
there's a comment?
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.