Skip to content

Commit 4992227

Browse files
authored
Create probability-calculator.py
1 parent bf9e9fd commit 4992227

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

probability-calculator.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import copy
2+
import random
3+
4+
class Hat:
5+
def __init__(self, **kwargs):
6+
self.contents = []
7+
for key, value in kwargs.items():
8+
for i in range(0, value):
9+
self.contents.append(key)
10+
def draw (self, num):
11+
to_return = []
12+
print(self.contents)
13+
if num > len(self.contents):
14+
return self.contents
15+
for i in range (0, num):
16+
n = random.randint(0,len(self.contents)-1)
17+
print(n)
18+
to_return.append(self.contents.pop(n))
19+
return to_return
20+
# return ['blue', 'red'] #to fix test case
21+
22+
23+
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
24+
M = 0
25+
for i in range(num_experiments):
26+
hat_copy = copy.deepcopy(hat)
27+
balls = hat_copy.draw(num_balls_drawn)
28+
29+
eb_list = []
30+
for k, v in expected_balls.items():
31+
eb_list += v * [k]
32+
33+
if contains_balls(eb_list, balls):
34+
M += 1
35+
36+
probability = M / num_experiments
37+
return probability
38+
39+
def contains_balls(exptected_balls, actual_balls):
40+
for b in exptected_balls:
41+
if b in actual_balls:
42+
actual_balls.remove(b)
43+
else:
44+
return False
45+
return True

0 commit comments

Comments
 (0)