Skip to content

Commit f0c319b

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

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
"",
8+
"The expected probability values should be compared using some tolerance to allow for small deviations."
9+
],
10+
"cases": [
11+
{
12+
"uuid": "f7b3eb26-bcfc-4c1e-a2de-af07afc33f45",
13+
"description": "matching birthday for same date",
14+
"property": "matchingBirthday",
15+
"input": {
16+
"birthday1": "2000-01-01",
17+
"birthday2": "2000-01-01"
18+
},
19+
"expected": true
20+
},
21+
{
22+
"uuid": "7193409a-6e16-4bcb-b4cc-9ffe55f79b25",
23+
"description": "matching birthday for same year and month but different day",
24+
"property": "matchingBirthday",
25+
"input": {
26+
"birthday1": "2012-05-09",
27+
"birthday2": "2012-05-17"
28+
},
29+
"expected": false
30+
},
31+
{
32+
"uuid": "d04db648-121b-4b72-93e8-d7d2dced4495",
33+
"description": "matching birthday for same month and day but different year",
34+
"property": "matchingBirthday",
35+
"input": {
36+
"birthday1": "1999-10-23",
37+
"birthday2": "1988-10-23"
38+
},
39+
"expected": true
40+
},
41+
{
42+
"uuid": "3c8bd0f0-14c6-4d4c-975a-4c636bfdc233",
43+
"description": "matching birthday for same year but different month and day",
44+
"property": "matchingBirthday",
45+
"input": {
46+
"birthday1": "2007-12-19",
47+
"birthday2": "2007-04-27"
48+
},
49+
"expected": false
50+
},
51+
{
52+
"uuid": "df5daba6-0879-4480-883c-e855c99cdaa3",
53+
"description": "matching birthday for different year month and day",
54+
"property": "matchingBirthday",
55+
"input": {
56+
"birthday1": "1997-08-04",
57+
"birthday2": "1963-11-23"
58+
},
59+
"expected": false
60+
},
61+
{
62+
"uuid": "70b38cea-d234-4697-b146-7d130cd4ee12",
63+
"description": "random birthdays return specified number of birthdays",
64+
"scenarios": ["random"],
65+
"property": "randomBirthdays",
66+
"input": {},
67+
"expected": "length == groupsize"
68+
},
69+
{
70+
"uuid": "d9d5b7d3-5fea-4752-b9c1-3fcd176d1b03",
71+
"description": "random birthdays have fixed year per batch",
72+
"scenarios": ["random"],
73+
"property": "randomBirthdays",
74+
"input": {},
75+
"expected": {
76+
"years": {
77+
"fixed": true
78+
}
79+
}
80+
},
81+
{
82+
"uuid": "8da69a53-6900-4b63-897d-9f025f149fd2",
83+
"description": "random birthdays have random year between batches",
84+
"scenarios": ["random"],
85+
"property": "randomBirthdays",
86+
"input": {},
87+
"expected": {
88+
"years": {
89+
"random": true
90+
}
91+
}
92+
},
93+
{
94+
"uuid": "d1074327-f68c-4c8a-b0ff-e3730d0f0521",
95+
"description": "random birthdays have random months",
96+
"scenarios": ["random"],
97+
"property": "randomBirthdays",
98+
"input": {},
99+
"expected": {
100+
"months": {
101+
"random": true
102+
}
103+
}
104+
},
105+
{
106+
"uuid": "7df706b3-c3f5-471d-9563-23a4d0577940",
107+
"description": "random birthdays have random days",
108+
"scenarios": ["random"],
109+
"property": "randomBirthdays",
110+
"input": {},
111+
"expected": {
112+
"days": {
113+
"random": true
114+
}
115+
}
116+
},
117+
{
118+
"uuid": "ade37c87-f41d-4929-962a-286b4d1d048a",
119+
"description": "has matching birthdays with matching birthday pair",
120+
"property": "hasMatchingBirthdays",
121+
"input": {
122+
"birthdays": ["1970-01-19", "1975-06-03", "2003-01-19"]
123+
},
124+
"expected": true
125+
},
126+
{
127+
"uuid": "1d155b33-c6e1-46b9-81fa-09123ae378ea",
128+
"description": "has matching birthdays without matching birthday pair",
129+
"property": "hasMatchingBirthdays",
130+
"input": {
131+
"birthdays": ["1984-04-05", "2000-09-17", "1966-10-12"]
132+
},
133+
"expected": false
134+
},
135+
{
136+
"uuid": "89a462a4-4265-4912-9506-fb027913f221",
137+
"description": "probability for matching birthday for one person",
138+
"scenarios": ["random"],
139+
"property": "probabilityForMatchingBirthdays",
140+
"input": {
141+
"groupSize": 1
142+
},
143+
"expected": 0.0
144+
},
145+
{
146+
"uuid": "ec31c787-0ebb-4548-970c-5dcb4eadfb5f",
147+
"description": "probability for matching birthday for ten people",
148+
"scenarios": ["random"],
149+
"property": "probabilityForMatchingBirthdays",
150+
"input": {
151+
"groupSize": 10
152+
},
153+
"expected": 11.7
154+
},
155+
{
156+
"uuid": "b548afac-a451-46a3-9bb0-cb1f60c48e2f",
157+
"description": "probability for matching birthday for twenty-three people",
158+
"scenarios": ["random"],
159+
"property": "probabilityForMatchingBirthdays",
160+
"input": {
161+
"groupSize": 23
162+
},
163+
"expected": 50.7
164+
},
165+
{
166+
"uuid": "e43e6b9d-d77b-4f6c-a960-0fc0129a0bc5",
167+
"description": "probability for matching birthday for seventy people",
168+
"scenarios": ["random"],
169+
"property": "probabilityForMatchingBirthdays",
170+
"input": {
171+
"groupSize": 70
172+
},
173+
"expected": 99.9
174+
}
175+
]
176+
}
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)