forked from ujjwalkarn/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_1_deck_of_cards.py
56 lines (41 loc) · 1.31 KB
/
7_1_deck_of_cards.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
#!/usr/bin/env python
"""
Design the data structure for a generic deck of cards.
Python Docs: http://docs.python.org/reference/datamodel.html
"""
class Card(object):
def __init__(self, card_value, suit=None, game_value=None):
face_cards = {'J': 11, 'Q': 12,
'K': 13, 'A': 1}
if game_value:
self.game_value = game_value
elif card_value in face_cards:
self.game_value = face_cards[card_value]
else:
self.game_value = card_value
self.card_value = str(card_value)
self.suit = suit
def __repr__(self):
if self.suit:
return ' '.join(['[', self.card_value, 'of', self.suit, ']'])
return ' '.join(['[', self.card_value, ']'])
def __cmp__(self, other):
"""Called by comparison operators."""
return self.game_value - other.game_value
def main():
jack = Card('J', 'clubs')
print jack
king = Card('K', 'hearts')
print king
print 'K > J:', king > jack
ten = Card(10, 'diamonds')
print ten
print '10 < J:', ten < jack
queen = Card('Q', 'spades')
print queen
print 'J < Q < K:', jack < queen < king
joker = Card('Joker', game_value=100)
print joker
print 'Joker > K:', joker > king
if __name__ == '__main__':
main()