Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python problems def prepdeck(): The prepdeck() function should prepare and retur

ID: 670528 • Letter: P

Question

Python problems

def prepdeck():
The prepdeck() function should prepare and return a representation of a standard 52 card deck. Just to
review, a deck of cards consists of 13 cards in each of 4 suits, spades, hearts, clubs and diamonds. 10 of
the 13 cards carry a nominal value (1 through 10) while the remaining three face cards have special
names: jack (11), queen (12) and king (13). For simplicity, we’ll refer to the cards by number, that is, 1
through 13 (n.b., not 0 through 12).
We therefore represent a deck as a list of tuples, where each tuple is of the form (n, s), where n is the
nominal value (an integer between 1 and 13, inclusive) and s is the suit (a string, ’spades’, ’hearts’,
’clubs’, or ’diamonds’). Creating a deck should be relatively easy using a list comprehension; but once
created, your function will also need to suffle and cut the deck (see the prior description by Montmort
given above). And while cutting the deck is pretty simple, shuffling a deck in true random order is
actually harder than it might first appear.
Shuffling the deck would seem to be fairly easy, but coming up with a mechanism to do so that ensures all
permutations of the 52 cards are equally likely to occur in practice turns out to be surprisingly hard. We’ll
use an algorithm attributed to Fisher, Yates and Knuth that is easy to implement and provides a fair
shuffle. The idea is to scan the sequence from that last L[len(L)-1] = L[-1] to the first L[1] while randomly
exchanging the current element with one of the elements before it. In other words, randomly exchange
each L[i] with an element between L[0] and L[i], inclusive.
For our source of randomness, we’ll use the random.randint() function (n.d.r., the random library also
includes a function random.shuffle() which you are not allowed to use for this assignment). The function
randint(lo,hi) returns an integer selected with uniform probability between the arguments lo and hi; but
unlike almost every other range specification in Python, these arguments are interpreted inclusively.

def treize(n=1, verbose=False):
This function plays n games of Treize. Each game pops cards off the deck, automatically introducing a
new shuffled deck when the original deck is exhausted. A game ends when the dealer’s count matches the
card dealt (a win for the dealer), or when the count reaches 13 (a loss for the dealer). The function returns a floating point number representing the ratio of dealer wins to games played.
The verbose argument should regulate the degree of trace output provided by the function. For example,
when verbose is True, my solution provides a running play-by-play description of the action:
>>> t r e i z e ( 2 , Tr ue )
1 : de a l i ng 11 of he a r t s
2 : de a l i ng 10 of di amo n d s
3 : de a l i ng 12 of cl ub s
4 : de a l i ng 3 of cl ub s
5 : de a l i ng 3 of he a r t s
6 : de a l i ng 5 of sp a d e s
7 : de a l i ng 5 of cl ub s
8 : de a l i ng 6 of di amo n d s
9 : de a l i ng 1 of cl ub s
1 0 : d e a l i ng 8 of sp a d e s
1 1 : d e a l i ng 8 of cl ub s
1 2 : d e a l i ng 13 of sp a d e s
1 3 : d e a l i ng 7 of cl ub s
De a l er lo s e s
1 : de a l i ng 4 of he a r t s
2 : de a l i ng 7 of sp a d e s
3 : de a l i ng 12 of di amo n d s
4 : de a l i ng 13 of he a r t s
5 : de a l i ng 12 of sp a d e s
6 : de a l i ng 2 of cl ub s
7 : de a l i ng 7 of di amo n d s
De a l er wi n s !
0 . 5
>>>
Note the winning percentage here is 50%, the returned value.

Explanation / Answer

def deck(): def build_deck(): suits = ["Hearts", "Diamonds", "Clubs", "Spades"] for suit in suits: for rank in range(2, 14): if rank == 11: value = "Jack" elif rank == 12: value = "Queen" elif rank == 13: value = "King" def shuffle_deck(deck): # Swaps values L[i] and L[j] def swap(L, i, j): temp = L[i] L[i] = L[j] L[j] = temp def gam_go(n=1, verbose= False): if verbose: print("Hello! Starting get_random()") list_of_nums= [] for i in range(n): newRand = random.randint(1,100) if verbose: print(" New random value:") list_of_nums.append(new_rand) if verbose: print("new stuff") return list_of_nums