From b1f2599a05f521453dd94c2cfe709000501cab4c Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sun, 31 Aug 2025 13:36:18 +0000 Subject: [PATCH] [Sync Iteration] python/black-jack/2 --- solutions/python/black-jack/2/black_jack.py | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 solutions/python/black-jack/2/black_jack.py diff --git a/solutions/python/black-jack/2/black_jack.py b/solutions/python/black-jack/2/black_jack.py new file mode 100644 index 0000000..2f96826 --- /dev/null +++ b/solutions/python/black-jack/2/black_jack.py @@ -0,0 +1,97 @@ +"""Functions to help play and score a game of blackjack. + +How to play blackjack: https://bicyclecards.com/how-to-play/blackjack/ +"Standard" playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck +""" +ace = ["A"] +faces = ["J", "Q", "K"] +numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "10"] +tens = [*faces, "10"] +double_down_points = [9, 10, 11] + +def value_of_card(card): + """Determine the scoring value of a card. + + :param card: str - given card. + :return: int - value of a given card. See below for values. + + 1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10 + 2. 'A' (ace card) = 1 + 3. '2' - '10' = numerical value. + """ + + if card in faces: + return 10 + if card in numbers: + return int(card) + return 1 + +def higher_card(card_one, card_two): + """Determine which card has a higher value in the hand. + + :param card_one, card_two: str - cards dealt in hand. See below for values. + :return: str or tuple - resulting Tuple contains both cards if they are of equal value. + + 1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10 + 2. 'A' (ace card) = 1 + 3. '2' - '10' = numerical value. + """ + + if value_of_card(card_one) > value_of_card(card_two): + return card_one + if value_of_card(card_one) < value_of_card(card_two): + return card_two + if value_of_card(card_one) == value_of_card(card_two): + return (card_one, card_two) + + +def value_of_ace(card_one, card_two): + """Calculate the most advantageous value for the ace card. + + :param card_one, card_two: str - card dealt. See below for values. + :return: int - either 1 or 11 value of the upcoming ace card. + + 1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10 + 2. 'A' (ace card) = 11 (if already in hand) + 3. '2' - '10' = numerical value. + """ + hand_value = value_of_card(card_one) + value_of_card(card_two) + if card_one == "A" or card_two == "A": + return 1 + if hand_value <= 10: + return 11 + else: + return 1 + +def is_blackjack(card_one, card_two): + """Determine if the hand is a 'natural' or 'blackjack'. + + :param card_one, card_two: str - card dealt. See below for values. + :return: bool - is the hand is a blackjack (two cards worth 21). + + 1. 'J', 'Q', or 'K' (otherwise known as "face cards") = 10 + 2. 'A' (ace card) = 11 (if already in hand) + 3. '2' - '10' = numerical value. + """ + + return bool((card_one == "A" or card_two == "A") and (card_one in tens or card_two in tens)) + + +def can_split_pairs(card_one, card_two): + """Determine if a player can split their hand into two hands. + + :param card_one, card_two: str - cards dealt. + :return: bool - can the hand be split into two pairs? (i.e. cards are of the same value). + """ + + return bool(value_of_card(card_one) == value_of_card(card_two)) + + +def can_double_down(card_one, card_two): + """Determine if a blackjack player can place a double down bet. + + :param card_one, card_two: str - first and second cards in hand. + :return: bool - can the hand can be doubled down? (i.e. totals 9, 10 or 11 points). + """ + hand_value = value_of_card(card_one) + value_of_card(card_two) + return bool(hand_value in double_down_points)