Guest User

Untitled

a guest
Nov 3rd, 2013
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import random
  3. import itertools
  4.  
  5. # the set of sets of letters on each card
  6. s = [['A','B','C'], ['D','E','F','G'], ['H','I','J','K','L']]
  7.  
  8. # the deck of all possible cards
  9. deck = list(itertools.product(*s))
  10.  
  11. # the full sequence of letters in the entire deck
  12. allchars = set(list(itertools.chain.from_iterable(s)))
  13.  
  14.  
  15. # Monte Carlo iteration size
  16. samplingsize = 1000000
  17.  
  18. # iterate through multiple hand sizes
  19. print "sampling size = ", samplingsize
  20. for k in xrange(5, 41):
  21.  
  22. # the size of the hand
  23. handsize = k
  24.  
  25. # count the number of hands that were a success
  26. handsuccess = 0
  27.  
  28. for j in xrange(0, samplingsize):
  29.  
  30. #pick a hand at random with unique cards
  31. thishand = random.sample(deck, handsize)
  32.  
  33. # get all unique chars in this hand
  34. thishandchars = set(list(itertools.chain.from_iterable(thishand)))
  35.  
  36. # compare the unique chars of the hand with the unique chars of the deck
  37. if(thishandchars == allchars):
  38. # hand contains all letters in deck
  39. handsuccess += 1;
  40.  
  41. print "For a hand size of {}, probability = {:.3f}%".format(handsize, 100.0 * handsuccess / samplingsize)
RAW Paste Data