Poker hand python 3.4 I need help with function poker_hand(cards): ’Straight’ (f
ID: 3810076 • Letter: P
Question
Poker hand python 3.4
I need help with function poker_hand(cards):
’Straight’ (five cards when rearranged will form a run of five consecutive cards)
’Four of a kind’ (four or five cards of the same rank)
’Three of a kind’ (exactly three cards of the same rank)
’One pair’ (at least one pair of the same rank, but not four cards of the same rank)
’High card’ (no cards of identical rank that also do not form a straight)
There's class to use.
Below is the examples.
I'm struggling with how to start, and develop the code for this.
Please help me.
The numbers given to the constructors below are in the range 0 through 51, as in the examples from lecture and the textbook. You will need to use the rank method of the Card class to extract the rank of each Card When you run the provided home work3.py file, you will see the actual rank and suit of each card printed on the screen Function Call Return Value an (Card (22) Card (16), Card (6) Card (17), Card (19) One pair' poker hand ([Card (22), Card (16), Card (9), Card (35), Card (19) j Three of a kind' poker-hand ([Card (51) Card (38) Card (6) Card (32) Card (12) j) Three of a kind poker-hand ([Card (11), Card (12) Card (14), Card (16), Card (30) High card' poker-hand [Card (3), Card (16), Card (17), Card (29), Card (42) j) Four of a kind' Straight. poker-hand [Card (3), Card (5), Card (6), Card (4), Card (7) poker-hand ([Card (11) Card (12) Card (13) Card (14) Card (15) J) High card.Explanation / Answer
class Card:
suit_sym = {0: 'u2663', 1: 'u2666', 2: 'u2665', 3: 'u2660'}
rank_sym = {0: '2', 1: '3', 2: '4', 3: '5', 4: '6', 5: '7', 6: '8',
7: '9', 8: '10', 9: 'J', 10: 'Q', 11: 'K', 12: 'A'}
def __init__(self, n):
self._id = n
def rank(self):
return self._id % 13
def suit(self):
return self._id // 13
def __repr__(self):
return Card.rank_sym[self.rank()] + Card.suit_sym[self.suit()]
def __lt__(self, other):
return self._id < other._id
def __eq__(self, other):
return self._id == other._id
def poker_hand(cards):
card_values = [card.rank() for card in cards]
card_values.sort()
if card_values == range(min(card_values), max(card_values)+1):
return "Straight"
four_of_a_kind = False
three_of_a_kind = False
> for i in card_values:
if card_values.count(i) == 4:
four_of_a_kind = True
elif card_values.count(i) == 3:
three_of_a_kind = True
elif card_values.count(i) == 2:
> if four_of_a_kind:
return "Four of a kind"
if three_of_a_kind:
return "Three of a kind"
if one_pair:
return "One pair"
return "High card"
print (poker_hand([Card(22), Card(16), Card(6), Card(17), Card(19)]))
print (poker_hand([Card(22), Card(16), Card(9), Card(35), Card(19)]))
print (poker_hand([Card(51), Card(38), Card(6), Card(32), Card(12)]))
print (poker_hand([Card(11), Card(12), Card(14), Card(16), Card(30)]))
print (poker_hand([Card(3), Card(16), Card(17), Card(29), Card(42)]))
print (poker_hand([Card(3), Card(5), Card(6), Card(4), Card(7)]))
print (poker_hand([Card(11), Card(12), Card(13), Card(14), Card(15)]))
# code link in case indentation mess up: https://goo.gl/zws1VZ