Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint # randint(1, 13) returns a random integer between 1 and 13 inclusive.
- from itertools import groupby # groups consecutive identical values of an iterable together
- N = 5000 # number of simulations
- def success(cards) :
- groups = list(list(g) for k,g in groupby(sorted(cards))) # ranks grouped...
- groups = list(reversed(sorted(groups, key=len))) # ... and sorted by decreasing length
- return len(groups[0]) == 3 and len(groups[1]) == 1 # implies len(groups[2]) == 1
- def test_success() :
- assert success([5,5,5,7,6])
- assert success([4,3,4,1,4])
- assert not success([1,2,3,4,5])
- assert not success([3,3,3,4,4])
- random_hand_ranks = lambda : [randint(1, 13) for _ in range(5)] # 5 random integers between 1 and 13 inclusive
- def sim(store=True) :
- x_coords = []
- y_coords = []
- cnt = 0
- for x in range(N) :
- if success(random_hand_ranks()) :
- cnt += 1
- if store :
- x_coords.append(x)
- y_coords.append(cnt)
- return (x_coords, y_coords) if store else cnt
- if input('Show graph? (Y)') == 'Y' :
- x,y = sim()
- import matplotlib.pyplot as plt
- plt.plot(x,y)
- plt.ylabel('Simulation succeses')
- plt.xlabel('Trials')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement