Last active
May 9, 2025 19:59
-
-
Save bocklund/79fa2c6fd06acdcfc2d80e79731e287b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(80, 1326, 0.06033182503770739)
(1640, 22100, 0.07420814479638009)