Skip to content

Instantly share code, notes, and snippets.

@bocklund
Last active May 9, 2025 19:59
Show Gist options
  • Select an option

  • Save bocklund/79fa2c6fd06acdcfc2d80e79731e287b to your computer and use it in GitHub Desktop.

Select an option

Save bocklund/79fa2c6fd06acdcfc2d80e79731e287b to your computer and use it in GitHub Desktop.
import itertools
cards = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
deck = 1*4*cards # 1 deck, 4 suits
def totals_for_hand(hand):
totals = [0]
for i, card in enumerate(hand):
new_totals = []
for j, t in enumerate(totals):
if card == "A":
new_totals.append(t + 1)
new_totals.append(t + 11)
elif card in {"J", "Q", "K"}:
new_totals.append(t + 10)
else:
new_totals.append(t + card)
totals = new_totals
return set(totals) # only care about unique totals
def count_hands_with_value(hands, value):
num_hands = len(hands)
num_hands_with_value = 0
for hand in hands:
if value in totals_for_hand(hand):
num_hands_with_value += 1
return num_hands_with_value, num_hands, num_hands_with_value / num_hands
hands = list(itertools.combinations(deck, 2))
hands_3_cards = list(itertools.combinations(deck, 3))
print(count_hands_with_value(hands, 19))
print(count_hands_with_value(hands_3_cards, 19))
@bocklund
Copy link
Author

bocklund commented May 9, 2025

(80, 1326, 0.06033182503770739)
(1640, 22100, 0.07420814479638009)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment