Skip to content

Instantly share code, notes, and snippets.

@tpott
Last active August 29, 2015 13:56
Show Gist options
  • Save tpott/9236434 to your computer and use it in GitHub Desktop.
Save tpott/9236434 to your computer and use it in GitHub Desktop.
# nhands.py
# Trevor Pottinger
# Fri Mar 14 22:23:19 PDT 2014
from random import random
def counts2deck(ccounts):
"""Converts the card counts to a list of sorted cards"""
deck = []
for letter in ccounts:
deck += [ letter for _ in range(ccounts[letter]) ]
return deck
def smalldeck():
"""Returns an example small deck"""
card_counts = {
'a' : 4,
'b' : 4,
'c' : 4,
'd' : 4,
'e' : 2
}
return counts2deck(card_counts)
def bigdeck():
"""Returns Troy's, more interesting, magic deck representation"""
troys_counts = {
'a' : 1, 'b' : 3, 'c' : 4, 'd' : 2,
'e' : 4, 'f' : 4, 'g' : 4, 'h' : 4,
'i' : 4, 'j' : 3, 'k' : 1, 'l' : 4,
'm' : 6, 'n' : 2, 'o' : 4, 'p' : 3,
'q' : 4, 'r' : 3
}
return counts2deck(troys_counts)
def shuffle(deck):
"""Returns a shuffled copy of the input list"""
copy = list(deck)
def sortrandom(x, y):
r = random()
if r < 0.5:
return 1
elif r > 0.5:
return -1
else:
return 0
copy.sort(sortrandom)
return copy
def draw(deck, count):
"""Shuffles the deck then returns the "top" cards"""
deck = shuffle(deck)
return deck[:count]
def hand2str(hand):
return "".join(hand)
def test(deck, start, end, magnitude, toprint=True):
for i in range(start, end + 1):
handcounts = {}
for _ in range(magnitude**i):
s = hand2str(draw(deck, i))
if s in handcounts:
handcounts[s] += 1
else:
handcounts[s] = 1
count = len(handcounts.keys())
if toprint:
print i, count
if __name__ == '__main__':
deck = bigdeck()
test(deck, 5, 7, 4)
# with the smalldeck, 1 .. 6, magnitude 10
# 1 4
# 2 24
# 3 120
# 4 607
# 5 2939
# 6 13979
# with bigdeck, 5 .. 7, magnitude 4
# 5 1023
# 6 4095
# 7 16383
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment