Skip to content

Instantly share code, notes, and snippets.

@kawa-kokosowa
Last active November 24, 2017 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawa-kokosowa/d35a1eb6e858acbbc8f148c9270e492f to your computer and use it in GitHub Desktop.
Save kawa-kokosowa/d35a1eb6e858acbbc8f148c9270e492f to your computer and use it in GitHub Desktop.
find combinations of two playing cards when summed equal that value
"""Find all the combinations of two cards being added
to equal a range of values 2-26, because 2 is the lowest
value that has a combination, and 26 is the highest value
which has a combination.
King 13
Queen 12
Jack 11
Ace 1
By Lily Seabreeze, thanks to Tom Almquist for helping
brainstorm the maths.
Outputs:
For the #2 there are 6 solutions for x+y with cards
For the #3 there are 32 solutions for x+y with cards
For the #4 there are 38 solutions for x+y with cards
For the #5 there are 64 solutions for x+y with cards
For the #6 there are 70 solutions for x+y with cards
For the #7 there are 96 solutions for x+y with cards
For the #8 there are 102 solutions for x+y with cards
For the #9 there are 128 solutions for x+y with cards
For the #10 there are 134 solutions for x+y with cards
For the #11 there are 160 solutions for x+y with cards
For the #12 there are 166 solutions for x+y with cards
For the #13 there are 192 solutions for x+y with cards
For the #14 there are 198 solutions for x+y with cards
For the #15 there are 192 solutions for x+y with cards
For the #16 there are 166 solutions for x+y with cards
For the #17 there are 160 solutions for x+y with cards
For the #18 there are 134 solutions for x+y with cards
For the #19 there are 128 solutions for x+y with cards
For the #20 there are 102 solutions for x+y with cards
For the #21 there are 96 solutions for x+y with cards
For the #22 there are 70 solutions for x+y with cards
For the #23 there are 64 solutions for x+y with cards
For the #24 there are 38 solutions for x+y with cards
For the #25 there are 32 solutions for x+y with cards
For the #26 there are 6 solutions for x+y with cards
"""
# effectively 1-13
all_possible_card_values = range(1, 14)
def get_card_combos(number):
combinations = 0
for x in all_possible_card_values:
for y in all_possible_card_values:
if x + y == number:
if x == y:
combinations += 6
else:
combinations += 16
return combinations
# Find all combinations for values 2-20
for meow in range(2, 27):
solutions = get_card_combos(meow)
print("For the #%d there are %d solutions for x+y with cards" % (meow, solutions))
@kawa-kokosowa
Copy link
Author

Looking back at this it's written very inefficiently, better to loop through just once building a counter dictionary where the key is the sum.

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