makispaiktis

BlackJack Count (Codewars)

Dec 2nd, 2019 (edited)
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. '''
  2.  
  3. Complete the function that determines the score of a hand in the card game Blackjack (aka 21).
  4.  
  5. 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).
  6.  
  7. Scoring rules:
  8. 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.
  9.  
  10. 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.
  11.  
  12. Examples
  13. ["A"]                           ==>  11
  14. ["A", "J"]                      ==>  21
  15. ["A", "10", "A"]                ==>  12
  16. ["5", "3", "7"]                 ==>  15
  17. ["5", "4", "3", "2", "A", "K"]  ==>  25
  18.  
  19.  
  20.  
  21. Test.assert_equals(score_hand(['2', '3']), 5)
  22. Test.assert_equals(score_hand(['7', '7', '8']), 22)
  23. Test.assert_equals(score_hand(['4', '7', '8']), 19)
  24. Test.assert_equals(score_hand(['K', 'J','Q']), 30)
  25. Test.assert_equals(score_hand(['A', '3']), 14)
  26. Test.assert_equals(score_hand(['A', 'A']), 12)
  27. Test.assert_equals(score_hand(['A', '2', 'A', '9', '9']), 22)
  28. '''
  29.  
  30.  
  31.  
  32. from itertools import permutations
  33. from itertools import combinations
  34.  
  35.  
  36. numbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
  37. figures = ["A", "K", "Q", "J"]
  38.  
  39. def score_hand(cards):
  40.  
  41.     sum = 0
  42.     for i in range(0, len(cards)):
  43.         if cards[i] in numbers:
  44.             sum += int(cards[i])
  45.         else:
  46.             if cards[i] == figures[0]:
  47.                 if sum <= 10:
  48.                     sum += 11
  49.                 else:
  50.                     sum += 1
  51.             else:
  52.                 sum += 10
  53.  
  54.     # Check the case when sum > 21
  55.     if sum <= 21:
  56.         return sum
  57.  
  58.     elif sum > 21:
  59.         # WHEN I HAVE THIS CASE I MUST DO THIS:
  60.         # GO AND CHECK ALL THE SUB-LISTS OF "CARDS" LIST THAT HAVE **** N - 1 **** ELEMENTS, **** IF CARDS LENGTH WAS N ****
  61.         # 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)
  62.         #
  63.         newSum = 0
  64.         cardsWithoutOneCardList = combinations(cards, len(cards)-1)
  65.         cardsWithoutTwoCardsList = combinations(cards, len(cards)-2)
  66.         isThereAScoreLessThan21 = False
  67.         for comboCardSubList in cardsWithoutOneCardList:
  68.             newSum = score_hand(comboCardSubList)
  69.             if newSum <= 21:
  70.                 isThereAScoreLessThan21 = True
  71.                 break
  72.  
  73.         # If there is a sum <= 21, I will return the FIRST sum
  74.         if isThereAScoreLessThan21:
  75.             return sum
  76.         else:
  77.             # I enter this else-case when I have not found score <= 21 in n-1 cards
  78.             # So, this case is when **** ALL CARD-COMBINATIONS WITH (N - 1) CARDS HAVE SUM > 21 ****
  79.             smallestSum = 1000
  80.             currentSum = 0
  81.             for subList in cardsWithoutOneCardList:
  82.                 currentSum = score_hand(subList)
  83.                 if currentSum < smallestSum and currentSum > 21:
  84.                     smallestSum = currentSum
  85.             for subList in cardsWithoutTwoCardsList:
  86.                 currentSum = score_hand(subList)
  87.                 if currentSum < smallestSum and currentSum > 21:
  88.                     smallestSum = currentSum
  89.             return smallestSum
  90.  
  91. # MAIN FUNCTION
  92. print(score_hand(['2', '3']))
  93. print(score_hand(['7', '7', '8']))
  94. print(score_hand(['4', '7', '8']))
  95. print(score_hand(['K', 'Q', 'J']))
  96. print(score_hand(['A', '3']))
  97. print(score_hand(['A', 'A']))
  98. print(score_hand(["5", "4", "3", "2", "A", "K"]))
  99. print(score_hand(['A', '2', 'A', '9', '9']))
  100.  
  101.  
  102.  
  103. '''
  104.  
  105. smallestSum = 100000
  106.        currentSum = 0
  107.        cardsMinusOneCardList = combinations(cards, len(cards)-1)
  108.        # The above list is a list with all the combinations of (n-1) cards, if I had n cards in the beginning
  109.        for subList in cardsMinusOneCardList:
  110.            currentSum = score_hand(subList)
  111.            if currentSum < smallestSum:
  112.                smallestSum = currentSum
  113.  
  114.        smallestSum2 = 100000
  115.        currentSum2 = 0
  116.        cardsMinusTwoCardsList = combinations(cards, len(cards) - 2)
  117.        # The above list is a list with all the combinations of (n-2) cards, if I had n cards in the beginning
  118.        for subList in cardsMinusOneCardList:
  119.            currentSum2 = score_hand(subList)
  120.            if currentSum2 < smallestSum2:
  121.                smallestSum2 = currentSum2
  122.  
  123.        if smallestSum < smallestSum2:
  124.            return smallestSum
  125.        else:
  126.            return smallestSum2
  127.  
  128. '''
Add Comment
Please, Sign In to add comment