Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import random
- import itertools
- # the set of sets of letters on each card
- s = [['A','B','C'], ['D','E','F','G'], ['H','I','J','K','L']]
- # the deck of all possible cards
- deck = list(itertools.product(*s))
- # the full sequence of letters in the entire deck
- allchars = set(list(itertools.chain.from_iterable(s)))
- # Monte Carlo iteration size
- samplingsize = 1000000
- # iterate through multiple hand sizes
- print "sampling size = ", samplingsize
- for k in xrange(5, 41):
- # the size of the hand
- handsize = k
- # count the number of hands that were a success
- handsuccess = 0
- for j in xrange(0, samplingsize):
- thishand = []
- for i in xrange(0,handsize):
- # pick a card at random, insert it into hand
- thishand.append(deck[random.randint(0,len(deck) - 1)])
- # get all unique chars in this hand
- thishandchars = set(list(itertools.chain.from_iterable(thishand)))
- # compare the unique chars of the hand with the unique chars of the deck
- if(thishandchars == allchars):
- # hand contains all letters in deck
- handsuccess += 1;
- print "For a hand size of {}, probability = {:.3f}%".format(handsize, 100.0 * handsuccess / samplingsize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement