Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
77 lines (54 loc) · 1.64 KB

File metadata and controls

77 lines (54 loc) · 1.64 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from __future__ import print_function, division
import random
def has_duplicates(t):
"""Returns True if any element appears more than once in a sequence.
t: list
returns: bool
"""
# make a copy of t to avoid modifying the parameter
s = t[:]
s.sort()
# check for adjacent elements that are equal
for i in range(len(s)-1):
if s[i] == s[i+1]:
return True
return False
def random_bdays(n):
"""Returns a list of integers between 1 and 365, with length n.
n: int
returns: list of int
"""
t = []
for i in range(n):
bday = random.randint(1, 365)
t.append(bday)
return t
def count_matches(num_students, num_simulations):
"""Generates a sample of birthdays and counts duplicates.
num_students: how many students in the group
num_samples: how many groups to simulate
returns: int
"""
count = 0
for i in range(num_simulations):
t = random_bdays(num_students)
if has_duplicates(t):
count += 1
return count
def main():
"""Runs the birthday simulation and prints the number of matches."""
num_students = 23
num_simulations = 1000
count = count_matches(num_students, num_simulations)
print('After %d simulations' % num_simulations)
print('with %d students' % num_students)
print('there were %d simulations with at least one match' % count)
if __name__ == '__main__':
main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.