Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Complete the function that determines the score of a hand in the card game Blackjack (aka 21).
- The function receives an array of strings that represent each card in the hand ("2", "3", ..., "10", "J", "Q", "K" or "A") and should return the score of the hand (integer).
- Scoring rules:
- Number cards count as their face value (2 through 10). Jack, Queen and King count as 10. An Ace can be counted as either 1 or 11.
- Return the highest score of the cards that is less than or equal to 21. If there is no score less than or equal to 21 return the smallest score more than 21.
- Examples
- ["A"] ==> 11
- ["A", "J"] ==> 21
- ["A", "10", "A"] ==> 12
- ["5", "3", "7"] ==> 15
- ["5", "4", "3", "2", "A", "K"] ==> 25
- Test.assert_equals(score_hand(['2', '3']), 5)
- Test.assert_equals(score_hand(['7', '7', '8']), 22)
- Test.assert_equals(score_hand(['4', '7', '8']), 19)
- Test.assert_equals(score_hand(['K', 'J','Q']), 30)
- Test.assert_equals(score_hand(['A', '3']), 14)
- Test.assert_equals(score_hand(['A', 'A']), 12)
- Test.assert_equals(score_hand(['A', '2', 'A', '9', '9']), 22)
- '''
- from itertools import permutations
- from itertools import combinations
- numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
- figures = ["A", "K", "Q", "J"]
- def score_hand(cards):
- sum = 0
- for i in range(0, len(cards)):
- if cards[i] in numbers:
- sum += int(cards[i])
- else:
- if cards[i] == figures[0]:
- if sum <= 10:
- sum += 11
- else:
- sum += 1
- else:
- sum += 10
- # Check the case when sum > 21
- if sum <= 21:
- return sum
- elif sum > 21:
- # WHEN I HAVE THIS CASE I MUST DO THIS:
- # GO AND CHECK ALL THE SUB-LISTS OF "CARDS" LIST THAT HAVE **** N - 1 **** ELEMENTS, **** IF CARDS LENGTH WAS N ****
- # If I find a combination of n-1 cards with sum <= 21, that means that I have to return the first sum I calculated (this one that was > 21)
- #
- newSum = 0
- cardsWithoutOneCardList = combinations(cards, len(cards)-1)
- cardsWithoutTwoCardsList = combinations(cards, len(cards)-2)
- isThereAScoreLessThan21 = False
- for comboCardSubList in cardsWithoutOneCardList:
- newSum = score_hand(comboCardSubList)
- if newSum <= 21:
- isThereAScoreLessThan21 = True
- break
- # If there is a sum <= 21, I will return the FIRST sum
- if isThereAScoreLessThan21:
- return sum
- else:
- # I enter this else-case when I have not found score <= 21 in n-1 cards
- # So, this case is when **** ALL CARD-COMBINATIONS WITH (N - 1) CARDS HAVE SUM > 21 ****
- smallestSum = 1000
- currentSum = 0
- for subList in cardsWithoutOneCardList:
- currentSum = score_hand(subList)
- if currentSum < smallestSum and currentSum > 21:
- smallestSum = currentSum
- for subList in cardsWithoutTwoCardsList:
- currentSum = score_hand(subList)
- if currentSum < smallestSum and currentSum > 21:
- smallestSum = currentSum
- return smallestSum
- # MAIN FUNCTION
- print(score_hand(['2', '3']))
- print(score_hand(['7', '7', '8']))
- print(score_hand(['4', '7', '8']))
- print(score_hand(['K', 'Q', 'J']))
- print(score_hand(['A', '3']))
- print(score_hand(['A', 'A']))
- print(score_hand(["5", "4", "3", "2", "A", "K"]))
- print(score_hand(['A', '2', 'A', '9', '9']))
- '''
- smallestSum = 100000
- currentSum = 0
- cardsMinusOneCardList = combinations(cards, len(cards)-1)
- # The above list is a list with all the combinations of (n-1) cards, if I had n cards in the beginning
- for subList in cardsMinusOneCardList:
- currentSum = score_hand(subList)
- if currentSum < smallestSum:
- smallestSum = currentSum
- smallestSum2 = 100000
- currentSum2 = 0
- cardsMinusTwoCardsList = combinations(cards, len(cards) - 2)
- # The above list is a list with all the combinations of (n-2) cards, if I had n cards in the beginning
- for subList in cardsMinusOneCardList:
- currentSum2 = score_hand(subList)
- if currentSum2 < smallestSum2:
- smallestSum2 = currentSum2
- if smallestSum < smallestSum2:
- return smallestSum
- else:
- return smallestSum2
- '''
Add Comment
Please, Sign In to add comment