-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrenchDeck.py
More file actions
30 lines (22 loc) · 833 Bytes
/
FrenchDeck.py
File metadata and controls
30 lines (22 loc) · 833 Bytes
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
import collections
Card = collections.namedtuple('Card', ['rank', 'suit'])
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
# print(self._cards)
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
def spades_high(card):
rank_value = FrenchDeck.ranks.index(card.rank)
return rank_value * len(suit_values) + suit_values[card.suit]
if __name__ == '__main__':
deck = FrenchDeck()
s = []
for card in sorted(deck, key=spades_high):
s.append(card)
# print(s)