Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions