Skip to content

Instantly share code, notes, and snippets.

@tpott
Last active August 29, 2015 13:57
Show Gist options
  • Save tpott/9562383 to your computer and use it in GitHub Desktop.
Save tpott/9562383 to your computer and use it in GitHub Desktop.
# nhands.py
# Trevor Pottinger
# Fri Mar 14 22:23:19 PDT 2014
import time
from random import random
def my_cmp(x,y):
if x[1] > y[1]:
return -1
elif x[1] < y[1]:
return 1
else:
return 0
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 shuf_and_draw(deck, count):
"""Shuffles the deck then returns the "top" cards"""
deck = shuffle(deck)
return deck[:count]
def hand2str(hand):
return "".join(hand)
def timed_counts(deck, hand_size):
start = time.time()
counts = handcounts(deck, hand_size)
print time.time() - start
return counts
def handcounts(deck, hand_size, num_of_draws=10000, order=False):
handcount = {}
for _ in range(num_of_draws):
hand = shuf_and_draw(deck, hand_size)
if not order:
hand.sort()
s = hand2str(hand)
if s in handcount:
handcount[s] += 1
else:
handcount[s] = 1
return handcount
def test(deck, start, end, magnitude, toprint=True, order=False):
"""deck is a list of cards
start .. end are the inclusive limits for the hand size
magnitude is for how many more tests need to be done for bigger hands
toprint is for actually printing the results
order specifies whether or not order matters"""
for i in range(start, end + 1):
example_handcounts = handcounts(deck, i, order, magnitude)
count = len(example_handcounts.keys())
if toprint:
print i, count
if __name__ == '__main__':
deck = bigdeck()
test(deck, 7, 7, 6)
# 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