-
Notifications
You must be signed in to change notification settings - Fork 4
/
birthdays.py
40 lines (32 loc) · 850 Bytes
/
birthdays.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
import random
def birthdays(n):
births = list()
for i in range(n):
births.append(random.randint(1, 365))
return births
def has_duplicates(l1):
l2 = list()
for i in l1:
if i not in l2:
l2.append(i)
else:
return True
return False
def prob(n, r):
count = 0
for i in range(r):
b = birthdays(n)
if has_duplicates(b) is True:
count += 1
print('The probability that at least one student has birthdays \n'
'on the same day, considering a class of {} students is: {:.2f}%'
.format(n, count/r * 100))
if __name__ == '__main__':
# r é o número de iterações
r = 10000
n = 41
# b = birthdays(n)
# print(b)
# print(has_duplicates([2, 3, 4]))
# print(has_duplicates([2, 3, 3]))
prob(n, r)