From 16b7dd053f980f2bd5d0da15c26f1bc5759e1b94 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Thu, 7 Aug 2025 07:12:17 +0000 Subject: [PATCH] [Sync Iteration] python/card-games/1 --- solutions/python/card-games/1/lists.py | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 solutions/python/card-games/1/lists.py diff --git a/solutions/python/card-games/1/lists.py b/solutions/python/card-games/1/lists.py new file mode 100644 index 0000000..4a0fd4d --- /dev/null +++ b/solutions/python/card-games/1/lists.py @@ -0,0 +1,88 @@ +""" +Functions for tracking poker hands and assorted card tasks. + +Python list documentation: https://docs.python.org/3/tutorial/datastructures.html +""" + + +def get_rounds(number: int) -> list[int]: + """ + Create a list containing the current and next two round numbers. + + :param number: int - current round number. + :return: list - current round and the two that follow. + """ + return [number, number + 1, number + 2] + + +def concatenate_rounds(rounds_1: list[int], + rounds_2: list[int]) -> list[int]: + """ + Concatenate two lists of round numbers. + + :param rounds_1: list - first rounds played. + :param rounds_2: list - second set of rounds played. + :return: list - all rounds played. + """ + return rounds_1 + rounds_2 + + +def list_contains_round(rounds: list[int], number: int) -> bool: + """ + Check if the list of rounds contains the specified number. + + :param rounds: list - rounds played. + :param number: int - round number. + :return: bool - was the round played? + """ + return number in rounds + + +def card_average(hand: list) -> float: + """ + Calculate and returns the average card value from the list. + + :param hand: list - cards in hand. + :return: float - average value of the cards in the hand. + """ + return sum(hand) / len(hand) + + +def approx_average_is_average(hand: list[int]) -> bool: + """ + Return if the (average of first and last card values) OR ('middle' card) == calculated average. + + :param hand: list - cards in hand. + :return: bool - does one of the approximate averages equal the `true average`? + """ + avg: float = card_average(hand) + return (hand[0] + hand[-1]) / 2 == avg or hand[len(hand) // 2] == avg + + +def average_even_is_average_odd(hand: list[int]) -> bool: + """ + Return if the (average of even indexed card values) == (average of odd indexed card values). + + :param hand: list - cards in hand. + :return: bool - are even and odd averages equal? + """ + even: list = [] + odd: list = [] + + for i, n in enumerate(hand): + if i % 2 == 0: + even.append(n) + else: + odd.append(n) + + return sum(even) / len(even) == sum(odd) / len(odd) + + +def maybe_double_last(hand: list[int]) -> list[int]: + """ + Multiply a Jack card value in the last index position by 2. + + :param hand: list - cards in hand. + :return: list - hand with Jacks (if present) value doubled. + """ + return hand if hand[-1] != 11 else hand[:-1] + [(hand[-1] * 2)]