-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob_calculator.py
86 lines (70 loc) · 2.21 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 5 20:25:56 2023
@author: user
"""
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self, **all_item):
#self.kwargs = kwargs
self.contents=[]
for key,value in all_item.items():
for itr in range(value):
self.contents.append(key)
def __str__(self):
s = ""
for i in self.contents:
s += i
s += " "
return s
def draw(self, amount):
drawn = []
if amount >= len(self.contents):
return self.contents
else:
for i in range(amount):
item = self.contents.pop(random.randrange(len(self.contents)))
drawn.append(item)
return drawn
def get_contents(self, *kwargs):
contents = []
for i in self.kwargs:
for k in range(self.kwargs[i]):
contents.append(i)
return contents
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
#print(expected)
m = 0
#drawn = hat.draw(num_balls_drawn)
for i in range(num_experiments-1):
#print(hat)
hat1 = copy.deepcopy(hat)
drawn = hat1.draw(num_balls_drawn)
drawn_dict = {}
'''for elem in drawn:
drawn_dict[elem] = drawn_dict.get(elem, 0) + 1
for elem in expected_balls:
if elem not in drawn_dict or expected_balls[elem] < drawn_dict[elem]:
break
#print(expected, drawn)
else:
m += 1'''
for key, value in expected_balls.items():
if drawn.count(key) < value:
break
else:
m +=1
return m/num_experiments
s = "good=3"
t = "verygood=2"
#print(contents)
hat = Hat(blue=3,red=2,green=6)
probability = experiment(hat=hat,
expected_balls={"blue":2,"green":1},
num_balls_drawn=4,
num_experiments=1000)
print(probability)
for i in range(3):
print(i)