-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob_calculator.py
57 lines (51 loc) · 2.12 KB
/
prob_calculator.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import copy
import random
# Consider using the modules imported above.
class Hat:
# kwargs is used when a function requires an indefinite number of variables. The ** sign indicates a variable in dictionary form
def __init__(self, **kwargs):
self.contents = []
self.number = 0
for key in kwargs.keys():
for i in range(0, kwargs.get(key)):
self.contents.append("%s" %key)
for value in kwargs.values():
self.number += value
def __str__(self):
return str(self.contents)
def draw(self, nbBall):
if nbBall > self.number:
return self.contents
if nbBall <= self.number:
self.taken = []
for i in range(0, nbBall, 1):
taken_id = random.randint(0, (len(self.contents)-1))
self.taken.append(self.contents.pop(taken_id))
return self.taken
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
exp = 0
exp_todo = num_experiments
success = 0
while exp < exp_todo:
# copy the class hat, so we do not modify the class directly
hat2 = copy.deepcopy(hat)
taken_list = hat2.draw(num_balls_drawn)
# convert the list of results into dictionary of balls' colors
taken = {i:taken_list.count(i) for i in taken_list}
# flag is a boolean var for indicating that the balls drawn contain all the expected balls
flag = False
# check if all colors in expected_balls match with those of the outcome of the draw
if (all (i in taken.keys() for i in expected_balls.keys())):
for key, value in expected_balls.items():
# check if the number of each color matches
if value <= taken.get(key):
flag = True
else:
flag = False
break
# if expected_balls is a subset of taken, then the outcome satisfies the requirement
if flag == True:
success += 1
exp_todo -= 1
probability = success / num_experiments
return probability