Poker haand: •Players have 5 cards in hand. •The rank of the hand depends on the
ID: 3802627 • Letter: P
Question
Poker haand:
•Players have 5 cards in hand.
•The rank of the hand depends on the patterns contained within
•Are all 5 in sequence?
•Are all 5 of the same suit?
•Are there matching values?
•Player with the highest ranked hand wins the round
Given 5 cards (user input), determine the rank of the hand.
Start by thinking through the process
Then make a python flowchart
Poker hand ranking:
Rank
Name
Description
9
Royal Flush
Straight (Ace, king, queen, jack, 10) + flush
8
Straight flush
Straight + flush
7
Four of a kind
Four equal value cards
6
Full House
One pair plus a different set of equal value cards
5
Flush
Five cards of the same suit
4
Straight
Five cards in sequence with no gaps
3
Three of a kind
One set of three cards of equal value
2
Two pairs
Two distinct pair of cards of equal value
1
One pair
One pair of equal value cards
0
Nothing
None of the qualities above
Rank
Name
Description
9
Royal Flush
Straight (Ace, king, queen, jack, 10) + flush
8
Straight flush
Straight + flush
7
Four of a kind
Four equal value cards
6
Full House
One pair plus a different set of equal value cards
5
Flush
Five cards of the same suit
4
Straight
Five cards in sequence with no gaps
3
Three of a kind
One set of three cards of equal value
2
Two pairs
Two distinct pair of cards of equal value
1
One pair
One pair of equal value cards
0
Nothing
None of the qualities above
Explanation / Answer
Below code contains a test case as well, to go with your typical poker game.
-------------------------------------------------------------------------
def poker(hands):
"Return the best hand: poker([hand,...]) => hand"
return max(hands, key=hand_rank)
def hand_rank(hand):
ranks = card_ranks(hand)
if straight(ranks) and flush(hand): # straight flush
return (8, max(ranks))
elif kind(4, ranks): # 4 of a kind
return (7, kind(4, ranks), kind(1, ranks))
elif kind(3, ranks) and kind(2, ranks): # full house
return (6,kind(3,ranks),kind(2,ranks))
elif flush(hand): # flush
return (5,ranks)
elif straight(ranks): # straight
return (4,max(ranks))
elif kind(3, ranks): # 3 of a kind
return (3,kind(3,ranks),ranks)
elif two_pair(ranks): # 2 pair
return (2,two_pair(ranks),ranks)
elif kind(2, ranks): # kind
return (1,kind(2,ranks),ranks)
else: # high card
return (0,ranks)
def card_ranks(cards):
"Return a list of the ranks,sorted with higher first"
ranks = ['--23456789TJQKA'.index(r) for r,s in cards]
ranks.sort(reverse = True)
return ranks
def straight(ranks):
"Return True if the ordered ranks from a 5-card straight"
return (max(ranks)-min(ranks) == 4) and len(set(ranks)) ==5
def flush(hand):
"Return True if all the cards have the same suit"
suits = [s for r,s in hand]
return len(set(suits)) == 1
def kind(n,ranks):
for r in ranks:
if ranks.count(r) == n: return r
return None
def two_pair(ranks):
pair = kind(2,ranks)
lowpair = kind(2,list(reversed(ranks)))
print (pair and lowpair)
if pair and lowpair !=pair:
return (pair,lowpair)
else:
return None
def test():
"Test cases for the functions in poker program"
sf = "6C 7C 8C 9C TC".split() # Straight Flush
fk = "9D 9H 9S 9C 7D".split() # Four of a Kind
fh = "TD TC TH 7C 7D".split() # Full House
assert card_ranks(sf) == [10,9,8,7,6]
assert card_ranks(fk) == [9,9,9,9,7]
assert card_ranks(fh) == [10,10,10,7,7]
assert poker([sf, fk, fh]) == sf
assert poker([fk, fh]) == fk
assert poker([fh, fh]) == fh
assert poker([sf]) == sf
assert poker([sf] + 99*[fh]) == sf
assert hand_rank(sf) == (8, 10)
assert hand_rank(fk) == (7, 9, 7)
assert hand_rank(fh) == (6, 10, 7)
return 'tests pass'
print test()