Skip to content

Instantly share code, notes, and snippets.

@ZechCodes
Created October 18, 2020 01:31
Show Gist options
  • Save ZechCodes/be2161259b249a40e910cfebc4f4b7d3 to your computer and use it in GitHub Desktop.
Save ZechCodes/be2161259b249a40e910cfebc4f4b7d3 to your computer and use it in GitHub Desktop.
Challenge 134 - Card Counting (BlackJack)

Challenge 134 - Card Counting (BlackJack)

In BlackJack, cards are counted with -1, 0, 1 values:

  • 2, 3, 4, 5, 6 are counted as +1
  • 7, 8, 9 are counted as 0
  • 10, J, Q, K, A are counted as -1

Create a function that counts the number and returns it from the list of cards provided.

Examples

count([5, 9, 10, 3, "J", "A", 4, 8, 5]) ➞ 1

count(["A", "A", "K", "Q", "Q", "J"]) ➞ -6

count(["A", 5, 5, 2, 6, 2, 3, 8, 9, 7]) ➞ 5

Notes

  • String inputs will always be upper case.
  • You do not need to consider case sensitivity.
  • If the argument is empty, return 0.
  • There will be no input other than: 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A".
from __future__ import annotations
from typing import List, Union
import unittest
Card = Union[int, str]
def count(cards: list[Card]) -> int:
return 0 # Put your code here!!!
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(1, count([5, 9, 10, 3, 'J', 'A', 4, 8, 5]))
def test_2(self):
self.assertEqual(-6, count(['A', 'A', 'K', 'Q', 'Q', 'J']))
def test_3(self):
self.assertEqual(5, count(['A', 5, 5, 2, 6, 2, 3, 8, 9, 7]))
def test_4(self):
self.assertEqual(8, count([2, 2, 2, 2, 2, 2, 2, 2]))
def test_5(self):
self.assertEqual(0, count([]))
def test_6(self):
self.assertEqual(-7, count(['A', 'A', 'A', 'A', 'A', 'A', 'A']))
def test_7(self):
self.assertEqual(0, count(['A', 'K', 'Q', 'J', 10, 9, 8, 7, 6, 5, 4, 3, 2]))
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment