From 674b5e3319b4b5870aa8d342ffc7abf2944a6293 Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Tue, 21 Sep 2021 15:07:42 +0300 Subject: [PATCH 1/7] Implement `black-jack` concept exercise --- .../concept/black-jack/.docs/instructions.md | 80 ++++++++---- .../concept/black-jack/.meta/exemplar.py | 75 ++++++++--- exercises/concept/black-jack/black_jack.py | 44 ++++++- .../concept/black-jack/black_jack_test.py | 122 ++++++++++++++---- 4 files changed, 252 insertions(+), 69 deletions(-) diff --git a/exercises/concept/black-jack/.docs/instructions.md b/exercises/concept/black-jack/.docs/instructions.md index b906923998..a45331a30a 100644 --- a/exercises/concept/black-jack/.docs/instructions.md +++ b/exercises/concept/black-jack/.docs/instructions.md @@ -1,49 +1,85 @@ -In this exercise, you're going to implement some rules from [Blackjack][blackjack] +# Instructions -You have some rules to implement for judging the result of the game. +In this exercise you are going to implement some rules of [Blackjack][blackjack], +such as the way the game is played and scored. -**Note** : In this exercise, _A_ means an ace, _J_ means jack, _Q_ means queen, and _K_ means king card. +**Note** : In this exercise, _A_ means ace, _J_ means jack, _Q_ means queen, and _K_ means king cards. -## 1. Calculate the number of card +## 1. Calculate the value of a card -Create the `number_of_card()` function with a parameter `card`. The value of _J, Q_ or _K_ is 10. If the `card` is _A_, then just return "ace". +In Blackjack, it is up to each individual player if an ace is worth 1 or 11 points. +Face cards are worth 10 points and any other card is worth its pip value. + +Define the `value_of_card` function with a parameter `card`. +The value of _J_, _Q_ or _K_ is 10. +Otherwise, return the pip value of a card. +Ace can take multiple values so let's ignore _A_ for the time being. ```python ->>> number_of_card('K') +>>> value_of_card('K') 10 ->>> number_of_card('A') -ace +>>> value_of_card('4') +4 ``` -## 2. Calculate the number of Ace - -Create the `number_of_ace()` function with a parameter `hand`. +## 2. Calculate the value of ace -1. `hand` : the sum of cards in hand with an ace. +As mentioned before, ace can be worth either 1 or 11 points. +At the same time, players try to get as close to 21 as possible, without going over 21. -Ace is 1 or 11. You have to decide the value of ace without the sum of hand exceeding 21. +Define the `value_of_ace` function with a parameter `hand_value`, +which is the total hand value before getting an ace card. +You have to decide how to score the upcoming ace card so that your hand won't exceed 21 if possible. ```python ->>> number_of_ace(19) +>>> value_of_ace(19) 1 ->>> number_of_ace(7) +>>> value_of_ace(7) 11 ``` -## 3. Judge Blackjack +## 3. Determine Blackjack + +If the first two cards are an ace and a ten-card, giving a count of 21 in two cards, it is known as blackjack. + +Define the `is_blackjack` function with a parameter `hand`, which is a pair of cards. +Determine if the hand is a blackjack. -Create the `blackjack()` function with a parameter `hand`. +**Note** : You can do this in many ways. If you can, use a way to check if there is an ace and a ten-card in the list. -1. `hand` : first two cards in hand. +```python +>>> is_blackjack(['A', 'K']) +True +>>> is_blackjack(['10', '9']) +False +``` + +## 4. Splitting pairs + +If the first two cards are of the same value, +such as two sixes, or a _Q_ and _K_ a player may choose to treat them as two separate hands. +It is known as splitting pairs. + +Define the `can_split_pairs` function with a parameter `hand`, which is a pair of cards. +Determine if the hand can be split into two pairs. +```python +>>> can_split_pairs(['Q', 'K']) +True +>>> can_split_pairs(['10', 'A']) +False +``` -This function should return if the hand is blackjack. There's must be an ace card in `hand`. +## 5. Doubling down -**Note** : If the player has an Ace and a ten-value card, it is called a _Blackjack_. Ten-value cards include _10, J, Q, K_. I think you may have many ways. But if you can, use a way to check if there are an ace and a ten-value in the list. +When the original two cards dealt total 9, 10, or 11 points +a player can place an additional bet equal to the original bet, known as doubling down. +Define the `can_double_down` function with a parameter `hand`, which is a pair of cards. +Determine if the hand can be doubled down. ```python ->>> blackjack(['A', 'K']) +>>> can_double_down(['A', '9']) True ->>> blackjack([10, 9]) +>>> can_double_down(['10', '2']) False ``` diff --git a/exercises/concept/black-jack/.meta/exemplar.py b/exercises/concept/black-jack/.meta/exemplar.py index 7276260e89..2740b25846 100644 --- a/exercises/concept/black-jack/.meta/exemplar.py +++ b/exercises/concept/black-jack/.meta/exemplar.py @@ -1,23 +1,66 @@ -def number_of_card(card): - if card == 'A': - return "ace" - elif card == 'J' or card == 'Q' or card == 'K': - return 10 +def value_of_card(card): + """ + + :param card: str - given card. + :return: int - value of a given card (face card = 10, pip value otherwise). + """ + + if card in ['J', 'Q', 'K']: + value = 10 else: - return card + value = int(card) + return value + + +def value_of_ace(hand_value): + """ + :param hand_value: int - current hand value. + :return: int - value of the upcoming ace card (either 1 or 11). + """ -def number_of_ace(hand): - if hand + 11 <= 21: - return 11 + if hand_value + 11 > 21: + value = 1 else: - return 1 + value = 11 + return value + + +def is_blackjack(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand is a blackjack (two cards worth 21). + """ + blackjack = False + if 'A' in hand: + if any(ten_card in hand for ten_card in ['10', 'J', 'Q', 'K']): + blackjack = True + return blackjack -def blackjack(hand): - if 'A' not in hand: - return False - elif 'J' in hand or 'Q' in hand or 'K' in hand or 10 in hand: - return True + +def can_split_pairs(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand can be split into two pairs (i.e. cards are of the same value). + """ + + card_one, card_two = hand + if 'A' in hand: + split_pairs = card_one == card_two else: - False + split_pairs = value_of_card(card_one) == value_of_card(card_two) + return split_pairs + + +def can_double_down(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand can be doubled down (i.e. totals 9, 10 or 11 points). + """ + + hand_value = sum(1 if card == 'A' else value_of_card(card) for card in hand) + return 9 <= hand_value <= 11 diff --git a/exercises/concept/black-jack/black_jack.py b/exercises/concept/black-jack/black_jack.py index 5c954d2d3f..b550895f16 100644 --- a/exercises/concept/black-jack/black_jack.py +++ b/exercises/concept/black-jack/black_jack.py @@ -1,10 +1,48 @@ -def number_of_card(card): +def value_of_card(card): + """ + + :param card: str - given card. + :return: int - value of a given card (face card = 10, pip value otherwise). + """ + + pass + + +def value_of_ace(hand_value): + """ + + :param hand_value: int - current hand value. + :return: int - value of the upcoming ace card (either 1 or 11). + """ + + pass + + +def is_blackjack(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand is a blackjack (two cards worth 21). + """ + pass -def number_of_ace(hand): +def can_split_pairs(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand can be split into two pairs (i.e. cards are of the same value). + """ + pass -def blackjack(hand): +def can_double_down(hand): + """ + + :param hand: list - a pair of cards in hand. + :return: bool - if the hand can be doubled down (i.e. totals 9, 10 or 11 points). + """ + pass diff --git a/exercises/concept/black-jack/black_jack_test.py b/exercises/concept/black-jack/black_jack_test.py index 0e12d27187..76be16bd8a 100644 --- a/exercises/concept/black-jack/black_jack_test.py +++ b/exercises/concept/black-jack/black_jack_test.py @@ -1,43 +1,109 @@ import unittest import pytest from black_jack import ( - number_of_card, - number_of_ace, - blackjack + value_of_card, + value_of_ace, + is_blackjack, + can_split_pairs, + can_double_down ) class BlackJackTest(unittest.TestCase): @pytest.mark.task(taskno=1) - def test_number_of_card(self): - input_data = ['K', 'A'] - output_data = [10, "ace"] - number_of_variants = range(1, len(input_data) + 1) + def test_value_of_card(self): + data = [ + ('2', 2), + ('5', 5), + ('8', 8), + ('10', 10), + ('J', 10), + ('Q', 10), + ('K', 10), + ] - for variant, input, output in zip(number_of_variants, input_data, output_data): - with self.subTest(f"variation #{variant}", input=input, output=output): - self.assertEqual(number_of_card(input[0], input[1]), output, - msg=f'Expected: {output} but the number of cards was calculated incorrectly.') + for variant, (card, value) in enumerate(data, 1): + with self.subTest(f'variation #{variant}', input=card, output=value): + self.assertEqual( + value, + value_of_card(card), + msg=f'Expected {value} as the value of {card}.' + ) @pytest.mark.task(taskno=2) - def test_number_of_ace(self): - input_data = [19, 7] - output_data = [1, 11] - number_of_variants = range(1, len(input_data) + 1) + def test_value_of_ace(self): + data = [ + (2, 11), + (5, 11), + (7, 11), + (9, 11), + (10, 11), + (11, 1), + (12, 1), + (15, 1), + (19, 1), + (20, 1), + ] - for variant, input, output in zip(number_of_variants, input_data, output_data): - with self.subTest(f"variation #{variant}", input=input, output=output): - self.assertEqual(number_of_ace(input[0], input[1]), output, - msg=f'Expected: {output} but the number of Ace cards was calculated incorrectly.') + for variant, (hand_value, ace_value) in enumerate(data, 1): + with self.subTest(f'variation #{variant}', input=hand_value, output=ace_value): + self.assertEqual( + ace_value, + value_of_ace(hand_value), + msg=f'Expected {ace_value} as the value of ace when the hand is worth {hand_value}.' + ) @pytest.mark.task(taskno=3) - def test_blackjack(self): - input_data = [['A', 'J'], [10, 9]] - output_data = [True, False] - number_of_variants = range(1, len(input_data) + 1) - - for variant, input, output in zip(number_of_variants, input_data, output_data): - with self.subTest(f"variation #{variant}", input=input, output=output): - self.assertEqual(blackjack(input[0], input[1]), output, - msg=f'Expected: {output} but the value returned was incorrect,') + def test_is_blackjack(self): + data = [ + (['A', 'K'], True), + (['10', 'A'], True), + (['10', '9'], False), + (['A', 'A'], False), + ] + + for variant, (hand, blackjack) in enumerate(data, 1): + with self.subTest(f'variation #{variant}', input=hand, output=blackjack): + self.assertEqual( + blackjack, + is_blackjack(hand), + msg=f'Hand {hand} {"is" if blackjack else "is not"} a blackjack.' + ) + + @pytest.mark.task(taskno=4) + def test_can_split_pairs(self): + data = [ + (['Q', 'K'], True), + (['6', '6'], True), + (['A', 'A'], True), + (['10', 'A'], False), + (['10', '9'], False), + ] + + for variant, (hand, split_pairs) in enumerate(data, 1): + with self.subTest(f'variation #{variant}', input=hand, output=split_pairs): + self.assertEqual( + split_pairs, + can_split_pairs(hand), + msg=f'Hand {hand} {"can" if split_pairs else "cannot"} be split into pairs.' + ) + + @pytest.mark.task(taskno=5) + def test_can_double_down(self): + data = [ + (['A', '9'], True), + (['K', 'A'], True), + (['4', '5'], True), + (['A', 'A'], False), + (['10', '2'], False), + (['10', '9'], False), + ] + + for variant, (hand, double_down) in enumerate(data, 1): + with self.subTest(f'variation #{variant}', input=hand, output=double_down): + self.assertEqual( + double_down, + can_double_down(hand), + msg=f'Hand {hand} {"can" if double_down else "cannot"} be doubled down.' + ) From 82f641a6caec9b15b6a94edd3aede168a6480d89 Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Wed, 22 Sep 2021 20:40:53 +0300 Subject: [PATCH 2/7] Refactor to not use lists --- .../concept/black-jack/.docs/instructions.md | 18 +++++----- .../concept/black-jack/.meta/exemplar.py | 34 ++++++++++-------- exercises/concept/black-jack/black_jack.py | 15 ++++---- .../concept/black-jack/black_jack_test.py | 36 +++++++++---------- 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/exercises/concept/black-jack/.docs/instructions.md b/exercises/concept/black-jack/.docs/instructions.md index a45331a30a..8de3624015 100644 --- a/exercises/concept/black-jack/.docs/instructions.md +++ b/exercises/concept/black-jack/.docs/instructions.md @@ -42,15 +42,15 @@ You have to decide how to score the upcoming ace card so that your hand won't ex If the first two cards are an ace and a ten-card, giving a count of 21 in two cards, it is known as blackjack. -Define the `is_blackjack` function with a parameter `hand`, which is a pair of cards. +Define the `is_blackjack` function with parameters `card_one` and `card_two`, which are a pair of cards. Determine if the hand is a blackjack. **Note** : You can do this in many ways. If you can, use a way to check if there is an ace and a ten-card in the list. ```python ->>> is_blackjack(['A', 'K']) +>>> is_blackjack('A', 'K') True ->>> is_blackjack(['10', '9']) +>>> is_blackjack('10', '9') False ``` @@ -60,12 +60,12 @@ If the first two cards are of the same value, such as two sixes, or a _Q_ and _K_ a player may choose to treat them as two separate hands. It is known as splitting pairs. -Define the `can_split_pairs` function with a parameter `hand`, which is a pair of cards. +Define the `can_split_pairs` function with parameters `card_one` and `card_two`, which are a pair of cards. Determine if the hand can be split into two pairs. ```python ->>> can_split_pairs(['Q', 'K']) +>>> can_split_pairs('Q', 'K') True ->>> can_split_pairs(['10', 'A']) +>>> can_split_pairs('10', 'A') False ``` @@ -74,12 +74,12 @@ False When the original two cards dealt total 9, 10, or 11 points a player can place an additional bet equal to the original bet, known as doubling down. -Define the `can_double_down` function with a parameter `hand`, which is a pair of cards. +Define the `can_double_down` function with parameters `card_one` and `card_two`, which are a pair of cards. Determine if the hand can be doubled down. ```python ->>> can_double_down(['A', '9']) +>>> can_double_down('A', '9') True ->>> can_double_down(['10', '2']) +>>> can_double_down('10', '2') False ``` diff --git a/exercises/concept/black-jack/.meta/exemplar.py b/exercises/concept/black-jack/.meta/exemplar.py index 2740b25846..bea4cbd489 100644 --- a/exercises/concept/black-jack/.meta/exemplar.py +++ b/exercises/concept/black-jack/.meta/exemplar.py @@ -5,7 +5,7 @@ def value_of_card(card): :return: int - value of a given card (face card = 10, pip value otherwise). """ - if card in ['J', 'Q', 'K']: + if card == 'J' or card == 'Q' or card == 'K': value = 10 else: value = int(card) @@ -26,41 +26,47 @@ def value_of_ace(hand_value): return value -def is_blackjack(hand): +def is_blackjack(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand is a blackjack (two cards worth 21). """ - blackjack = False - if 'A' in hand: - if any(ten_card in hand for ten_card in ['10', 'J', 'Q', 'K']): - blackjack = True + if card_one == 'A' and card_two != 'A': + blackjack = value_of_card(card_two) == 10 + elif card_one != 'A' and card_two == 'A': + blackjack = value_of_card(card_one) == 10 + else: + blackjack = False return blackjack -def can_split_pairs(hand): +def can_split_pairs(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand can be split into two pairs (i.e. cards are of the same value). """ - card_one, card_two = hand - if 'A' in hand: + if card_one == 'A' or card_two == 'A': split_pairs = card_one == card_two else: split_pairs = value_of_card(card_one) == value_of_card(card_two) return split_pairs -def can_double_down(hand): +def can_double_down(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand can be doubled down (i.e. totals 9, 10 or 11 points). """ - hand_value = sum(1 if card == 'A' else value_of_card(card) for card in hand) + card_one_value = 1 if card_one == 'A' else value_of_card(card_one) + card_two_value = 1 if card_two == 'A' else value_of_card(card_two) + hand_value = card_one_value + card_two_value return 9 <= hand_value <= 11 diff --git a/exercises/concept/black-jack/black_jack.py b/exercises/concept/black-jack/black_jack.py index b550895f16..dbc68c3faf 100644 --- a/exercises/concept/black-jack/black_jack.py +++ b/exercises/concept/black-jack/black_jack.py @@ -18,30 +18,33 @@ def value_of_ace(hand_value): pass -def is_blackjack(hand): +def is_blackjack(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand is a blackjack (two cards worth 21). """ pass -def can_split_pairs(hand): +def can_split_pairs(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand can be split into two pairs (i.e. cards are of the same value). """ pass -def can_double_down(hand): +def can_double_down(card_one, card_two): """ - :param hand: list - a pair of cards in hand. + :param card_one: str - first card in hand. + :param card_two: str - second card in hand. :return: bool - if the hand can be doubled down (i.e. totals 9, 10 or 11 points). """ diff --git a/exercises/concept/black-jack/black_jack_test.py b/exercises/concept/black-jack/black_jack_test.py index 76be16bd8a..bfef90bff5 100644 --- a/exercises/concept/black-jack/black_jack_test.py +++ b/exercises/concept/black-jack/black_jack_test.py @@ -57,53 +57,53 @@ def test_value_of_ace(self): @pytest.mark.task(taskno=3) def test_is_blackjack(self): data = [ - (['A', 'K'], True), - (['10', 'A'], True), - (['10', '9'], False), - (['A', 'A'], False), + (('A', 'K'), True), + (('10', 'A'), True), + (('10', '9'), False), + (('A', 'A'), False), ] for variant, (hand, blackjack) in enumerate(data, 1): with self.subTest(f'variation #{variant}', input=hand, output=blackjack): self.assertEqual( blackjack, - is_blackjack(hand), + is_blackjack(*hand), msg=f'Hand {hand} {"is" if blackjack else "is not"} a blackjack.' ) @pytest.mark.task(taskno=4) def test_can_split_pairs(self): data = [ - (['Q', 'K'], True), - (['6', '6'], True), - (['A', 'A'], True), - (['10', 'A'], False), - (['10', '9'], False), + (('Q', 'K'), True), + (('6', '6'), True), + (('A', 'A'), True), + (('10', 'A'), False), + (('10', '9'), False), ] for variant, (hand, split_pairs) in enumerate(data, 1): with self.subTest(f'variation #{variant}', input=hand, output=split_pairs): self.assertEqual( split_pairs, - can_split_pairs(hand), + can_split_pairs(*hand), msg=f'Hand {hand} {"can" if split_pairs else "cannot"} be split into pairs.' ) @pytest.mark.task(taskno=5) def test_can_double_down(self): data = [ - (['A', '9'], True), - (['K', 'A'], True), - (['4', '5'], True), - (['A', 'A'], False), - (['10', '2'], False), - (['10', '9'], False), + (('A', '9'), True), + (('K', 'A'), True), + (('4', '5'), True), + (('A', 'A'), False), + (('10', '2'), False), + (('10', '9'), False), ] for variant, (hand, double_down) in enumerate(data, 1): with self.subTest(f'variation #{variant}', input=hand, output=double_down): self.assertEqual( double_down, - can_double_down(hand), + can_double_down(*hand), msg=f'Hand {hand} {"can" if double_down else "cannot"} be doubled down.' ) From 453b22056fa740f06f6345264c92d89e1a4804ac Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Mon, 27 Sep 2021 15:40:26 +0300 Subject: [PATCH 3/7] Clarify wording around a standard card deck --- exercises/concept/black-jack/.docs/instructions.md | 8 +++++--- exercises/concept/black-jack/.meta/exemplar.py | 2 +- exercises/concept/black-jack/black_jack.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/exercises/concept/black-jack/.docs/instructions.md b/exercises/concept/black-jack/.docs/instructions.md index 8de3624015..8c389bc54c 100644 --- a/exercises/concept/black-jack/.docs/instructions.md +++ b/exercises/concept/black-jack/.docs/instructions.md @@ -4,15 +4,16 @@ In this exercise you are going to implement some rules of [Blackjack][blackjack] such as the way the game is played and scored. **Note** : In this exercise, _A_ means ace, _J_ means jack, _Q_ means queen, and _K_ means king cards. +A [standard 52-card deck][standard_deck] is assumed. ## 1. Calculate the value of a card In Blackjack, it is up to each individual player if an ace is worth 1 or 11 points. -Face cards are worth 10 points and any other card is worth its pip value. +Face cards (_J_, _Q_, _K_) are worth 10 points and any other card is worth its pip (numerical) value. Define the `value_of_card` function with a parameter `card`. The value of _J_, _Q_ or _K_ is 10. -Otherwise, return the pip value of a card. +Otherwise, return the numerical value of a card. Ace can take multiple values so let's ignore _A_ for the time being. ```python @@ -83,4 +84,5 @@ True False ``` -[blackjack]: https://en.wikipedia.org/wiki/Blackjack \ No newline at end of file +[blackjack]: https://en.wikipedia.org/wiki/Blackjack +[standard_deck]: https://en.wikipedia.org/wiki/Standard_52-card_deck diff --git a/exercises/concept/black-jack/.meta/exemplar.py b/exercises/concept/black-jack/.meta/exemplar.py index bea4cbd489..0b6c7b3208 100644 --- a/exercises/concept/black-jack/.meta/exemplar.py +++ b/exercises/concept/black-jack/.meta/exemplar.py @@ -2,7 +2,7 @@ def value_of_card(card): """ :param card: str - given card. - :return: int - value of a given card (face card = 10, pip value otherwise). + :return: int - value of a given card (J, Q, K = 10, numerical value otherwise). """ if card == 'J' or card == 'Q' or card == 'K': diff --git a/exercises/concept/black-jack/black_jack.py b/exercises/concept/black-jack/black_jack.py index dbc68c3faf..7343184f97 100644 --- a/exercises/concept/black-jack/black_jack.py +++ b/exercises/concept/black-jack/black_jack.py @@ -2,7 +2,7 @@ def value_of_card(card): """ :param card: str - given card. - :return: int - value of a given card (face card = 10, pip value otherwise). + :return: int - value of a given card (J, Q, K = 10, numerical value otherwise). """ pass From 5d18e9b0c8ab9ead4213d74c4a24ce2bd4a5f7f2 Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Mon, 27 Sep 2021 15:50:20 +0300 Subject: [PATCH 4/7] Apply phrasing suggestions from code review Co-authored-by: BethanyG --- .../concept/black-jack/.docs/instructions.md | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/exercises/concept/black-jack/.docs/instructions.md b/exercises/concept/black-jack/.docs/instructions.md index 8c389bc54c..774b35fdb4 100644 --- a/exercises/concept/black-jack/.docs/instructions.md +++ b/exercises/concept/black-jack/.docs/instructions.md @@ -14,7 +14,7 @@ Face cards (_J_, _Q_, _K_) are worth 10 points and any other card is worth its p Define the `value_of_card` function with a parameter `card`. The value of _J_, _Q_ or _K_ is 10. Otherwise, return the numerical value of a card. -Ace can take multiple values so let's ignore _A_ for the time being. +An ace can take on multiple values so let's ignore _A_ for the time being. ```python >>> value_of_card('K') @@ -23,14 +23,15 @@ Ace can take multiple values so let's ignore _A_ for the time being. 4 ``` -## 2. Calculate the value of ace +## 2. Calculate the value of an ace -As mentioned before, ace can be worth either 1 or 11 points. -At the same time, players try to get as close to 21 as possible, without going over 21. +As mentioned before, an ace can be worth _either_ 1 or 11 points. +At the same time, players are trying to get as close to 21 as possible, without going _over_ 21. -Define the `value_of_ace` function with a parameter `hand_value`, +Define the `value_of_ace()` function with a parameter `hand_value`, which is the total hand value before getting an ace card. -You have to decide how to score the upcoming ace card so that your hand won't exceed 21 if possible. +You now have to decide if the upcoming ace card will be scored as a 1 or 11. +The value of the hand with the ace needs to be as high as possible _without_ going over 21. ```python >>> value_of_ace(19) @@ -43,10 +44,11 @@ You have to decide how to score the upcoming ace card so that your hand won't ex If the first two cards are an ace and a ten-card, giving a count of 21 in two cards, it is known as blackjack. -Define the `is_blackjack` function with parameters `card_one` and `card_two`, which are a pair of cards. -Determine if the hand is a blackjack. +Define the `is_blackjack(, )` function with parameters `card_one` and `card_two`, which are a pair of cards. +Determine if the two-card hand is a blackjack. -**Note** : You can do this in many ways. If you can, use a way to check if there is an ace and a ten-card in the list. +**Note** : This calculation can be done in many ways. + If possible, check if there is an ace and a ten-card in the hand. ```python >>> is_blackjack('A', 'K') @@ -59,10 +61,10 @@ False If the first two cards are of the same value, such as two sixes, or a _Q_ and _K_ a player may choose to treat them as two separate hands. -It is known as splitting pairs. +This is known as "splitting pairs". -Define the `can_split_pairs` function with parameters `card_one` and `card_two`, which are a pair of cards. -Determine if the hand can be split into two pairs. +Define the `can_split_pairs(, )` function with parameters `card_one` and `card_two`, which are a pair of cards. +Determine if this two-card hand can be split into two pairs. ```python >>> can_split_pairs('Q', 'K') True @@ -73,10 +75,11 @@ False ## 5. Doubling down When the original two cards dealt total 9, 10, or 11 points -a player can place an additional bet equal to the original bet, known as doubling down. +a player can place an additional bet equal to the original bet. +This is known as "doubling down". -Define the `can_double_down` function with parameters `card_one` and `card_two`, which are a pair of cards. -Determine if the hand can be doubled down. +Define the `can_double_down(, )` function with parameters `card_one` and `card_two`, which are a pair of cards. +Determine if the two-card hand can be "doubled down". ```python >>> can_double_down('A', '9') True From 7a8865729b4c4f428da82928edd5b7bcfaf75d91 Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Mon, 27 Sep 2021 17:03:23 +0300 Subject: [PATCH 5/7] Supply hints --- exercises/concept/black-jack/.docs/hints.md | 34 ++++++++++++++------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/exercises/concept/black-jack/.docs/hints.md b/exercises/concept/black-jack/.docs/hints.md index 48d5446c44..f91a51a182 100644 --- a/exercises/concept/black-jack/.docs/hints.md +++ b/exercises/concept/black-jack/.docs/hints.md @@ -1,21 +1,33 @@ # General -- [The Python Comparisons Tutorial][the python comparisons tutorial] and [Python comparisons examples][python comparisons examples] can be a great introduction. +[The Python comparisons tutorial][python comparisons tutorial] and [Python comparisons examples][python comparisons examples] are a great introduction covering the content of this exercise. -## 1. Calculate the number of card +## 1. Calculate the value of a card -- You can use the [equality comparison operator][equality comparison operator] to get the number of the card. +- You can use the equality comparison operator `==` to determine specific cards, e.g. `card == 'J'`. +- You can use the [`int` constructor][int constructor] to get an integer number from an integer literal, e.g. `int(card)`. -## 2. Calculate the number of Ace +## 2. Calculate the value of an ace -- You can use the [order comparisons operator][order comparisons operator]to decide the value of ace without the sum of hand exceeding 21. +- You can use the order comparison operator `>` to decide the appropriate course of action, e.g. `hand_value + 11 > 21`. -## 3. Judge Blackjack +## 3. Determine Blackjack -- You can use the [membership test operations][membership test operations] in `if` or `elif` syntax to find black-jack from the first two cards in your hand. +- You can use the [`if`/`elif`/`else` syntax][if syntax] to handle different combinations of cards. +- You can reuse the already implemented `value_of_card` function. -[the python comparisons tutorial]: https://docs.python.org/3/reference/expressions.html#comparisons +## 4. Splitting pairs + +- You can handle the `A` case (when at least one of the cards in an ace) separately. + +## 5. Doubling down + +- You can chain comparison operators, e.g. `9 <= hand_value <= 11`. +- You can use the [conditional expression][conditional expression] (sometimes called a "ternary operator") +to shorten simple `if`/`else` statements, e.g. `1 if card == 'A' else value_of_card(card)`. + +[python comparisons tutorial]: https://docs.python.org/3/reference/expressions.html#comparisons [python comparisons examples]: https://www.tutorialspoint.com/python/comparison_operators_example.htm -[equality comparison operator]: https://docs.python.org/3/reference/expressions.html#comparisons -[order comparisons operator]: https://docs.python.org/3/reference/expressions.html#comparisons -[membership test operations]: https://docs.python.org/3/reference/expressions.html#comparisons \ No newline at end of file +[int constructor]: https://docs.python.org/3/library/functions.html#int +[if syntax]: https://docs.python.org/3/tutorial/controlflow.html#if-statements +[conditional expression]: https://docs.python.org/3/reference/expressions.html#conditional-expressions From c48b27f567b87813f40217bbfb62949f88aa8888 Mon Sep 17 00:00:00 2001 From: Pranas Ziaukas Date: Mon, 27 Sep 2021 17:09:06 +0300 Subject: [PATCH 6/7] Claim co-authorship --- exercises/concept/black-jack/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/black-jack/.meta/config.json b/exercises/concept/black-jack/.meta/config.json index e5e5971ea3..22dac30f5b 100644 --- a/exercises/concept/black-jack/.meta/config.json +++ b/exercises/concept/black-jack/.meta/config.json @@ -1,6 +1,6 @@ { "blurb": "Learn about comparisons by implementing some Black Jack judging rules.", - "authors": ["Ticktakto", "Yabby1997", "limm-jk", "OMEGA-Y", "wnstj2007"], + "authors": ["Ticktakto", "Yabby1997", "limm-jk", "OMEGA-Y", "wnstj2007", "pranasas"], "icon": "poker", "contributors": ["bethanyg"], "files": { From 1daf2a996bb9c06ce99552f6b76f845138db0873 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Mon, 27 Sep 2021 09:21:48 -0700 Subject: [PATCH 7/7] Update exercises/concept/black-jack/.meta/config.json --- exercises/concept/black-jack/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/black-jack/.meta/config.json b/exercises/concept/black-jack/.meta/config.json index 22dac30f5b..c120dafa28 100644 --- a/exercises/concept/black-jack/.meta/config.json +++ b/exercises/concept/black-jack/.meta/config.json @@ -1,6 +1,6 @@ { "blurb": "Learn about comparisons by implementing some Black Jack judging rules.", - "authors": ["Ticktakto", "Yabby1997", "limm-jk", "OMEGA-Y", "wnstj2007", "pranasas"], + "authors": ["Ticktakto", "Yabby1997", "limm-jk", "OMEGA-Y", "wnstj2007", "pranasziaukas"], "icon": "poker", "contributors": ["bethanyg"], "files": {