-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_hand.py
68 lines (56 loc) · 1.77 KB
/
draw_hand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import random
RANKS = range(1,14)
SUITS = ('Spades', 'Hearts', 'Clubs', 'Diamonds')
RANK_VALUES = {
1: 'Ace',
11: 'Jack',
12: 'Queen',
13: 'King',
}
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.name = RANK_VALUES.get(rank) or rank
def __repr__(self):
return f'{self.name} of {self.suit}'
def create_deck():
""" Returns a standard deck of playing cards as a list.
"""
return [Card(rank, suit)
for suit in SUITS
for rank in RANKS]
def draw_hand(deck, hand_size=5):
""" Draw hand_size cards from the top of the deck.
Assume that the deck can accomodate the hand size.
Returns the drawn hand and the remainder of the deck.
"""
return deck[:hand_size], deck[hand_size:]
def contains_pair(hand):
""" Returns a boolean indicating whether or
not the hand has at least two cards of the same rank.
"""
return len(set(card.rank for card in hand)) != len(hand)
def verify_deck_size(deck, size):
""" Verify that the deck is of a certain size.
Error out if this verification fails.
"""
if len(deck) != size:
raise RuntimeException("Error")
def count_suits(hand):
""" Return a mapping of suit:count for each of the
four suits based on the cards in hand.
"""
suits = {suit: 0 for suit in SUITS}
for suit in (card.suit for card in hand):
suits[suit] += 1
return suits
if __name__ == "__main__":
deck = create_deck()
random.shuffle(deck)
hand, deck = draw_hand(deck)
verify_deck_size(deck, 47)
print("Hand : %s" % hand)
print("Contains pair : %s" % contains_pair(hand))
print("Suit counts : %s" % count_suits(hand))