PLEASE CODE IN PYTHON Write the function poker hand() that takes a list of exact
ID: 3817813 • Letter: P
Question
PLEASE CODE IN PYTHON
Write the function poker hand() that takes a list of exactly five Card objects as an argument, analyzes the list, and returns one of the following strings that describes the hand:
• ’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)
The string ’High card’ is returned when no pair, triple or quadruple is found in the list of cards. The returned string may be capitalized any way you wish, but no characters (including spaces) may otherwise be added or changed.
If two pairs are present in the hand, the function should simply return ’One pair’. If a pair of one rank and a three-of-a-kind of another rank are present in the hand, the function should simply return ’Three of a kind’. The Card class that you will find in homework3.py is the same class we studied in lecture. Use it to complete this part of the assignment. You may add additional instance variables or methods to the class if you want.
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 homework3.py file, you will see the actual rank and suit of each card printed on the screen.
Function Call : Return Value
poker hand([Card(22), Card(16), Card(6), Card(17), Card(19)]) ’One pair’
poker hand([Card(22), Card(16), Card(9), Card(35), Card(19)]) ’Three of a kind’
poker hand([Card(51), Card(38), Card(6), Card(32), Card(12)]) ’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)]) ’Four of a kind’
poker hand([Card(3), Card(5), Card(6), Card(4), Card(7)]) ’Straight’
poker hand([Card(11), Card(12), Card(13), Card(14), Card(15)]) ’High card’
PROVIDED BELOW IS THE CARD CLASS TO BE USED IN THE PROBLEM
Explanation / Answer
# pastebin link: https://pastebin.com/L6t9CePP
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 check_straight(cards):
straight_with_ace = [0,1,2,3,12]
if cards == straight_with_ace:
return True
cards[3] = 11
if cards == straight_with_ace:
return True
cards[2] = 10
if cards == straight_with_ace:
return True
cards[1] = 9
if cards == straight_with_ace:
return True
return False
def all_same_suit(suit):
return suit.count(suit[0]) == 5
def poker_hand(cards):
card_values = [card.rank() for card in cards]
card_values.sort()
suit = [card.suit() for card in cards]
if all_same_suit(suit):
if card_values == range(min(card_values), max(card_values)+1):
return "Straight"
if card_values[0] == 0 and card_values[-1] == 12:
if check_straight(card_values):
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"
if __name__ == "__main__":
hand1 = [Card(22), Card(16), Card(6), Card(17), Card(19)]
print('Testing poker_hand() for ' + str(hand1) + ': ' + str(poker_hand(hand1)))
hand2 = [Card(22), Card(16), Card(9), Card(35), Card(19)]
print('Testing poker_hand() for ' + str(hand2) + ': ' + str(poker_hand(hand2)))
hand3 = [Card(51), Card(38), Card(6), Card(32), Card(12)]
print('Testing poker_hand() for ' + str(hand3) + ': ' + str(poker_hand(hand3)))
hand4 = [Card(11), Card(12), Card(14), Card(16), Card(30)]
print('Testing poker_hand() for ' + str(hand4) + ': ' + str(poker_hand(hand4)))
hand5 = [Card(3), Card(16), Card(17), Card(29), Card(42)]
print('Testing poker_hand() for ' + str(hand5) + ': ' + str(poker_hand(hand5)))
hand6 = [Card(3), Card(5), Card(6), Card(4), Card(7)]
print('Testing poker_hand() for ' + str(hand6) + ': ' + str(poker_hand(hand6)))
hand7 = [Card(11), Card(12), Card(13), Card(14), Card(15)]
print('Testing poker_hand() for ' + str(hand7) + ': ' + str(poker_hand(hand7)))
hand8 = [Card(5), Card(7), Card(8), Card(6), Card(9)]
print('Testing poker_hand() for ' + str(hand8) + ': ' + str(poker_hand(hand8)))
-----------------------------------------------------------
Please leave positive rating
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.