|
| 1 | +from collections import Counter |
| 2 | +import random |
| 3 | + |
| 4 | +# Constants |
| 5 | +HAND_RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] |
| 6 | +SUITS = ['H', 'D', 'C', 'S'] |
| 7 | + |
| 8 | + |
| 9 | +def f_681(): |
| 10 | + """ |
| 11 | + Generate a random poker hand consisting of five cards, and count the frequency of each card rank. |
| 12 | +
|
| 13 | + The function creates a list of five cards where each card is a string made up of a rank and a suit (e.g., "10H" for Ten of Hearts). |
| 14 | + It then counts the frequency of each card rank in the hand using a Counter dictionary. |
| 15 | +
|
| 16 | + Parameters: |
| 17 | + - None |
| 18 | +
|
| 19 | + Returns: |
| 20 | + tuple: A tuple containing two elements: |
| 21 | + - hand (list): A list of five cards. |
| 22 | + - rank_count (counter): A Counter dictionary of card ranks with their frequencies in the hand. |
| 23 | +
|
| 24 | + Requirements: |
| 25 | + - collections |
| 26 | + - random |
| 27 | +
|
| 28 | + Example: |
| 29 | + >>> random.seed(42) |
| 30 | + >>> hand, rank_counts = f_681() |
| 31 | + >>> print(hand) |
| 32 | + ['QH', '2C', '5D', '4H', 'QH'] |
| 33 | + >>> print(rank_counts) |
| 34 | + Counter({'Q': 2, '2': 1, '5': 1, '4': 1}) |
| 35 | + """ |
| 36 | + |
| 37 | + hand = [] |
| 38 | + for _ in range(5): |
| 39 | + rank = random.choice(HAND_RANKS) |
| 40 | + suit = random.choice(SUITS) |
| 41 | + card = f'{rank}{suit}' |
| 42 | + hand.append(card) |
| 43 | + |
| 44 | + rank_counts = Counter([card[:-1] for card in hand]) |
| 45 | + |
| 46 | + return hand, rank_counts |
| 47 | + |
| 48 | + |
| 49 | +import unittest |
| 50 | +from collections import Counter |
| 51 | + |
| 52 | +HAND_RANKS = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] |
| 53 | +SUITS = ['H', 'D', 'C', 'S'] |
| 54 | + |
| 55 | + |
| 56 | +class TestCases(unittest.TestCase): |
| 57 | + def setUp(self) -> None: |
| 58 | + random.seed(42) |
| 59 | + |
| 60 | + def test_poker_hand_length(self): |
| 61 | + """Test if the poker hand has 5 cards.""" |
| 62 | + hand, rank_counts = f_681() |
| 63 | + self.assertEqual(len(hand), 5, "The poker hand should contain 5 cards.") |
| 64 | + |
| 65 | + def test_card_format(self): |
| 66 | + """Test if each card in the hand is formatted correctly.""" |
| 67 | + hand, rank_counts = f_681() |
| 68 | + for card in hand: |
| 69 | + self.assertIn(len(card), [2, 3], |
| 70 | + "Each card should be a string of length 2 or 3.") |
| 71 | + self.assertIn(card[:-1], HAND_RANKS, |
| 72 | + "The rank of each card should be valid.") |
| 73 | + self.assertIn(card[-1], SUITS, "The suit of each card should be valid.") |
| 74 | + |
| 75 | + def test_rank_counts_type(self): |
| 76 | + """Test if rank_counts is of type Counter.""" |
| 77 | + hand, rank_counts = f_681() |
| 78 | + self.assertIsInstance(rank_counts, Counter, |
| 79 | + "rank_counts should be a Counter dictionary.") |
| 80 | + |
| 81 | + def test_rank_counts_keys(self): |
| 82 | + """Test if the keys of rank_counts are valid ranks.""" |
| 83 | + hand, rank_counts = f_681() |
| 84 | + for rank in rank_counts.keys(): |
| 85 | + self.assertIn(rank, HAND_RANKS, "The ranks in rank_counts should be valid.") |
| 86 | + |
| 87 | + def test_rank_counts_values(self): |
| 88 | + """Test if the values of rank_counts are integers.""" |
| 89 | + hand, rank_counts = f_681() |
| 90 | + for count in rank_counts.values(): |
| 91 | + self.assertIsInstance(count, int, |
| 92 | + "The counts in rank_counts should be integers.") |
| 93 | + |
| 94 | + |
| 95 | +def run_tests(): |
| 96 | + suite = unittest.TestSuite() |
| 97 | + suite.addTest(unittest.makeSuite(TestCases)) |
| 98 | + runner = unittest.TextTestRunner() |
| 99 | + runner.run(suite) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + run_tests() |
0 commit comments