Skip to content

Commit 10e63ab

Browse files
Add baffling-birthdays exercise
1 parent 37a7bb2 commit 10e63ab

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
{
2+
"exercise": "baffling-birthdays",
3+
"comments": [
4+
"Dates are formatted as 'YYYY-MM-DD'.",
5+
"",
6+
"To increase the likelihood of consistent result, the tests should run the code returning random results many times.",
7+
"The expected probability values should be compared using some tolerance to allow for small deviations."
8+
],
9+
"cases": [
10+
{
11+
"uuid": "f7b3eb26-bcfc-4c1e-a2de-af07afc33f45",
12+
"description": "matching birthday for same date",
13+
"property": "matchingBirthday",
14+
"input": {
15+
"birthday1": "2000-01-01",
16+
"birthday2": "2000-01-01"
17+
},
18+
"expected": true
19+
},
20+
{
21+
"uuid": "7193409a-6e16-4bcb-b4cc-9ffe55f79b25",
22+
"description": "matching birthday for same year and month but different day",
23+
"property": "matchingBirthday",
24+
"input": {
25+
"birthday1": "2012-05-09",
26+
"birthday2": "2012-05-17"
27+
},
28+
"expected": false
29+
},
30+
{
31+
"uuid": "d04db648-121b-4b72-93e8-d7d2dced4495",
32+
"description": "matching birthday for same month and day but different year",
33+
"property": "matchingBirthday",
34+
"input": {
35+
"birthday1": "1999-10-23",
36+
"birthday2": "1988-10-23"
37+
},
38+
"expected": true
39+
},
40+
{
41+
"uuid": "3c8bd0f0-14c6-4d4c-975a-4c636bfdc233",
42+
"description": "matching birthday for same year but different month and day",
43+
"property": "matchingBirthday",
44+
"input": {
45+
"birthday1": "2007-12-19",
46+
"birthday2": "2007-04-27"
47+
},
48+
"expected": false
49+
},
50+
{
51+
"uuid": "df5daba6-0879-4480-883c-e855c99cdaa3",
52+
"description": "matching birthday for different year month and day",
53+
"property": "matchingBirthday",
54+
"input": {
55+
"birthday1": "1997-08-04",
56+
"birthday2": "1963-11-23"
57+
},
58+
"expected": false
59+
},
60+
{
61+
"uuid": "70b38cea-d234-4697-b146-7d130cd4ee12",
62+
"description": "random birthdays return specified number of birthdays",
63+
"scenarios": ["random"],
64+
"property": "randomBirthdays",
65+
"input": {},
66+
"expected": "length == groupsize"
67+
},
68+
{
69+
"uuid": "d9d5b7d3-5fea-4752-b9c1-3fcd176d1b03",
70+
"description": "random birthdays have fixed year per batch",
71+
"scenarios": ["random"],
72+
"property": "randomBirthdays",
73+
"input": {},
74+
"expected": {
75+
"years": {
76+
"fixed": true
77+
}
78+
}
79+
},
80+
{
81+
"uuid": "8da69a53-6900-4b63-897d-9f025f149fd2",
82+
"description": "random birthdays have random year between batches",
83+
"scenarios": ["random"],
84+
"property": "randomBirthdays",
85+
"input": {},
86+
"expected": {
87+
"years": {
88+
"random": true
89+
}
90+
}
91+
},
92+
{
93+
"uuid": "d1074327-f68c-4c8a-b0ff-e3730d0f0521",
94+
"description": "random birthdays have random months",
95+
"scenarios": ["random"],
96+
"property": "randomBirthdays",
97+
"input": {},
98+
"expected": {
99+
"months": {
100+
"random": true
101+
}
102+
}
103+
},
104+
{
105+
"uuid": "7df706b3-c3f5-471d-9563-23a4d0577940",
106+
"description": "random birthdays have random days",
107+
"scenarios": ["random"],
108+
"property": "randomBirthdays",
109+
"input": {},
110+
"expected": {
111+
"days": {
112+
"random": true
113+
}
114+
}
115+
},
116+
{
117+
"uuid": "ade37c87-f41d-4929-962a-286b4d1d048a",
118+
"description": "has matching birthdays with matching birthday pair",
119+
"property": "hasMatchingBirthdays",
120+
"input": {
121+
"birthdays": ["1970-01-19", "1975-06-03", "2003-01-19"]
122+
},
123+
"expected": true
124+
},
125+
{
126+
"uuid": "1d155b33-c6e1-46b9-81fa-09123ae378ea",
127+
"description": "has matching birthdays without matching birthday pair",
128+
"property": "hasMatchingBirthdays",
129+
"input": {
130+
"birthdays": ["1984-04-05", "2000-09-17", "1966-10-12"]
131+
},
132+
"expected": false
133+
},
134+
{
135+
"uuid": "89a462a4-4265-4912-9506-fb027913f221",
136+
"description": "probability for matching birthday for one person",
137+
"scenarios": ["random"],
138+
"property": "probabilityForMatchingBirthdays",
139+
"input": {
140+
"groupSize": 1
141+
},
142+
"expected": 0.0
143+
},
144+
{
145+
"uuid": "ec31c787-0ebb-4548-970c-5dcb4eadfb5f",
146+
"description": "probability for matching birthday for ten people",
147+
"scenarios": ["random"],
148+
"property": "probabilityForMatchingBirthdays",
149+
"input": {
150+
"groupSize": 10
151+
},
152+
"expected": 11.7
153+
},
154+
{
155+
"uuid": "b548afac-a451-46a3-9bb0-cb1f60c48e2f",
156+
"description": "probability for matching birthday for twenty-three people",
157+
"scenarios": ["random"],
158+
"property": "probabilityForMatchingBirthdays",
159+
"input": {
160+
"groupSize": 23
161+
},
162+
"expected": 50.7
163+
},
164+
{
165+
"uuid": "e43e6b9d-d77b-4f6c-a960-0fc0129a0bc5",
166+
"description": "probability for matching birthday for seventy people",
167+
"scenarios": ["random"],
168+
"property": "probabilityForMatchingBirthdays",
169+
"input": {
170+
"groupSize": 70
171+
},
172+
"expected": 99.9
173+
}
174+
]
175+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Instructions
2+
3+
Your task is to implement a solution that verifies the Birthday Problem's probabilities.
4+
5+
To do this, you need to:
6+
7+
- Determine if two birthdates match (same month and day).
8+
- Generate random birthdates.
9+
- Check if a set of randomly generated birthdates contains at least one matching pair.
10+
- Calculate the probability of at least one match for different group sizes.
11+
12+
~~~~exercism/caution
13+
The Birthday Problem assumes that birthdays are uniformly distributed within a single year.
14+
While the year should vary between different random birthdate generation calls, all birthdates within a single call must share the same year.
15+
~~~~
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Introduction
2+
3+
Fresh out of graduation, you're throwing a huge party to celebrate with friends and family.
4+
Over 70 people have shown up, including your mildly eccentric Uncle Ted.
5+
6+
In one of his usual antics, he bets you £100 that at least two people in the room share the same birthday.
7+
That sounds ridiculous—there are 365 possible birthdays, so you confidently accept.
8+
9+
To your astonishment, after collecting just 32 birthdays, you've already found a match.
10+
Magnanimous, you hand Uncle Ted his £100, but something feels off.
11+
12+
The next day, curiosity gets the better of you.
13+
A quick web search leads you to the [Birthday Problem][birthday-problem], which reveals that with just 23 people, the probability of a shared birthday exceeds 50%.
14+
15+
Ah. So _that's_ why Uncle Ted was so confident.
16+
17+
Determined to turn the tables, you start looking up other paradoxes—next time, _you'll_ be the one making the bets.
18+
19+
~~~~exercism/note
20+
The birthday paradox is a veridical paradox: even though it feels wrong, it is actually true.
21+
22+
[veridical-paradox]: https://en.wikipedia.org/wiki/Paradox#Quine's_classification
23+
~~~~
24+
25+
[birthday-problem]: https://en.wikipedia.org/wiki/Birthday_problem
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
title = "Baffling Birthdays"
2+
blurb = "Verify the Birthday Problem's probabilities."
3+
source = "Erik Schierboom"

0 commit comments

Comments
 (0)