Question: PYTHON ASSIGNMENT UML Diagrams: (1) In the card.py module, add __lt__ and __eq__ methods to the existing Card class. The Card instance methods __lt__ and
PYTHON ASSIGNMENT

UML Diagrams:
(1) In the card.py module, add __lt__ and __eq__ methods to the existing Card class. The Card instance methods__lt__ and __eq__ should return self.rank , and self.rank == other.rank, respectively.
(2) Optional: add a __repr__ method to the Card class so that the output of print(cards_array) is something like this:
[2H, KS, 10D]
(3) In the test2a.py file, write unit tests to test the __lt__ and __eq__ methods in Item 2. Use a copy the module constants.py, which contains constants for the PokerHand class.
(4) Implement a PokerHand class in the pokerhand.py module that inherits from the base class Deck. Use these import statements at the top of the pokerhand.py module:
from card import Card from deck import Deck from constants import *
(5) Define an __init__ method with the header
def __init__(self, the_array):
that (i) initializes the _cards instance variable to [ ], (ii) appends each Card object in the_array to the_cards array, (iii) initializes the instance variable hand_type to UNCLASSIFIED.Implement the instance method classify, which determines the hand type of the PokerHand object. This source code contains the complete source code to check for a hand of type FOUR_OF_A_KIND and hints for completing the other hand types:
def classify(self): self._cards.sort( ) if # ... code to classify hand of type STRAIGHT_FLUSH: elif self._cards[0] == self._cards[1] and \ self._cards[1] == self._cards[2] and \ self._cards[2] == self._cards[3]: self.hand_type = FOUR_OF_A_KIND elif self._cards[1] == self._cards[2] and \ self._cards[2] == self._cards[3] and \ self._cards[3] == self._cards[4]: self.hand_type = FOUR_OF_A_KIND elif # ... code to classify hand of type FULL_HOUSE: elif # ... code to classify hand of type FLUSH: elif # ... code to classify hand of type STRAIGHT: elif # ... code to classify hand of type THREE_OF_A_KIND: elif # ... code to classify hand of type TWO_PAIR: elif # ... code to classify hand of type ONE_PAIR: else: self.hand_type = NO_PAIR
Note: self._cards.sort( ) works because you added the __lt__ method to the card class.
(6) In the test2b.py test file, write unit tests to test your PokerHand class.
(7) In the test3.py file, create a new array like this:
counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
This counts array will keep track of how many poker hands of each type are obtained. In the test3.py file, repeat the following 2,000 times:
(8) Create a new deck object.
(9) Shuffle the deck object.
(10) Deal out five poker hands. Use an array to hold the five PokerHand objects. Classify the five poker hands by calling the PokerHand#classify method for each hand.
(11) Update the counts array like this:
for ph in poker_hands counts[ph.hand_type] += 1 end
(12) Compare your results to the expected values shown in the Poker Hands in Rank Order section. (13) Here is some pseudocode that you can translate. Recall that if you want the range to be the values 0, 1, 2, 3, 4, you need to use range(0, 5). 0 is the starting point and 5 is the length of the range.
import constants module Create array named counts, initialized to [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] repeat 2,000 times: Create a new Deck object. Call it deck. Shuffle the deck object. Create an an empty array like this: poker_hands = [ ] repeat 5 times: Create an empty array called arr. repeat 5 times: Deal a card from deck and append it to arr. end Create a new PokerHand object ph using the cards in arr and append ph to poker_hands. end end for each ph in the poker_hands array: invoke the classify method of ph. end for each ph in the poker_hands array: counts[ph.hand_type] += 1 end end Print out the number of occurances of each hand type.
Here is an example of the beginning of the output printing the number of occurances of each hand type:
print("Number of Occurrences of each Hand Type") print("=======================================") print(f"Straight Flush: {counts[STRAIGHT_FLUSH]}") print(f"Four of a Kind: {counts[FOUR_OF_A_KIND]}") . Goal: Implement and test a PokerHand class. Prerequisites: The Card and Deck classes from the Deck Example. . Deliverables: card.py deck.py test2a.py constants .pv pokerhand.py test2b.py test3 . py . Poker Hands in Rank Order: Hand Type Expected Frequency per 10,000 Hands No Pair One Pair Two Pairs Three of a Kind Straight Flush Full House Four of a Kind Straight Flush 5,012 4,225 475 211 39.2 19.6 14.4 2.4 0.14 . Goal: Implement and test a PokerHand class. Prerequisites: The Card and Deck classes from the Deck Example. . Deliverables: card.py deck.py test2a.py constants .pv pokerhand.py test2b.py test3 . py . Poker Hands in Rank Order: Hand Type Expected Frequency per 10,000 Hands No Pair One Pair Two Pairs Three of a Kind Straight Flush Full House Four of a Kind Straight Flush 5,012 4,225 475 211 39.2 19.6 14.4 2.4 0.14 Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
