-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobability-calculator-projec.py
45 lines (29 loc) · 1.12 KB
/
probability-calculator-projec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import copy
import random
from typing import Dict
class Hat:
def __init__(self,**kwargs:Dict[str,int]):
self.contents=[key for key in kwargs.keys() for _ in range(kwargs[key])]
def draw(self, number:int):
if number > len(self.contents):
draw_balls = self.contents[:]
self.contents.clear()
return draw_balls
else:
draw_balls = []
for _ in range(number):
i_random = random.randint(0,len(self.contents)-1)
draw_balls.append(self.contents[i_random])
self.contents.pop(i_random)
return draw_balls
def experiment(hat: Hat, expected_balls: Dict[str,int], num_balls_drawn: int, num_experiments:int):
count=0
for _ in range(num_experiments):
copy_hat = copy.deepcopy(hat)
balls_drawn = copy_hat.draw(num_balls_drawn)
for key,value in expected_balls.items():
if balls_drawn.count(key) < value:
break
else:
count+=1
return count/num_experiments