Advertisement
Programmin-in-Python

Sample Space of Tossing n Coins

Dec 19th, 2020 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. from random import shuffle
  2.  
  3. def cointoss(n):
  4.     h, t = 'H', 'T'
  5.     SampleS = [h*n, t*n]
  6.  
  7.     if n == 1:
  8.         print(SampleS)
  9.  
  10.     else:
  11.         L1 = list(h*n)+list(t*n)
  12.  
  13.         while True:
  14.             shuffle(L1)
  15.  
  16.             event = L1[:n]         
  17.             str1 = ''
  18.  
  19.             for i in event:
  20.                 str1 += i
  21.  
  22.             else:
  23.                 if str1 not in SampleS:
  24.                     SampleS.append(str1)
  25.                     str1 = ''
  26.  
  27.                 else:
  28.                     continue
  29.  
  30.             if len(SampleS) == 2**n:
  31.                 break
  32.  
  33.         SampleS.sort()
  34.  
  35.         for i in ('H','T'):
  36.             for j in SampleS:
  37.                 if i == j[0]:
  38.                     print(j, end=", ")
  39.             else:
  40.                 print()
  41.  
  42. cointoss(4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement