Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions bin/generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

local json = require 'dkjson'

local utils = {}

utils.map = function(t, f)
local mapped = {}
for i, v in ipairs(t) do
mapped[i] = f(v)
end
return mapped
end

utils.stringify = function(s)
s = s:gsub('\n', '\\n')
if s:match("'") and not s:match('"') then
return '"' .. s .. '"'
else
return "'" .. s:gsub("'", "\\'") .. "'"
end
end

utils.snake_case = function(str)
local s = str:gsub('%u', function(c)
return '_' .. c:lower()
end)
if s:sub(1, 1) == '_' then
s = s:sub(2)
end
return s
end

package.loaded['utils'] = utils
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it so that they can be loaded via require 'utils' without creating a file to contain them

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I might steal that for moonscript.


local function read_file(path)
local f = assert(io.open(path, 'r'))
local contents = f:read('*a')
Expand Down Expand Up @@ -81,12 +112,12 @@ local function process(node)
return table.concat(output, '\n')
else
local template = [[
it('%s', function()
it(%s, function()
%s
end)]]

return template:format(
node.description:lower():gsub('\'', '\\\''),
utils.stringify(node.description:lower()),
spec_generator.generate_test(node)
)
end
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,14 @@
"exception_handling",
"graphs"
]
},
{
"slug": "baffling-birthdays",
"name": "Baffling Birthdays",
"uuid": "54dd8d8f-b58c-47d0-8c77-22dd5fcdf14f",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
Expand Down
10 changes: 2 additions & 8 deletions exercises/practice/anagram/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
local function map(t, f)
local mapped = {}
for i, v in ipairs(t) do
mapped[i] = f(v)
end
return mapped
end
local utils = require 'utils'

local function render_list(list)
return '{ ' .. table.concat(map(list, function(v)
return '{ ' .. table.concat(utils.map(list, function(v)
return "'" .. v .. "'"
end), ', ') .. ' }'
end
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/baffling-birthdays/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
23 changes: 23 additions & 0 deletions exercises/practice/baffling-birthdays/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Instructions

Your task is to estimate the birthday paradox's probabilities.

To do this, you need to:

- Generate random birthdates.
- Check if a collection of randomly generated birthdates contains at least two with the same birthday.
- Estimate the probability that at least two people in a group share the same birthday for different group sizes.

~~~~exercism/note
A birthdate includes the full date of birth (year, month, and day), whereas a birthday refers only to the month and day, which repeat each year.
Two birthdates with the same month and day correspond to the same birthday.
~~~~

~~~~exercism/caution
The birthday paradox assumes that:

- There are 365 possible birthdays (no leap years).
- Each birthday is equally likely (uniform distribution).

Your implementation must follow these assumptions.
~~~~
25 changes: 25 additions & 0 deletions exercises/practice/baffling-birthdays/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Introduction

Fresh out of college, you're throwing a huge party to celebrate with friends and family.
Over 70 people have shown up, including your mildly eccentric Uncle Ted.

In one of his usual antics, he bets you £100 that at least two people in the room share the same birthday.
That sounds ridiculous — there are many more possible birthdays than there are guests, so you confidently accept.

To your astonishment, after collecting the birthdays of just 32 guests, you've already found two guests that share the same birthday.
Accepting your loss, you hand Uncle Ted his £100, but something feels off.

The next day, curiosity gets the better of you.
A quick web search leads you to the [birthday paradox][birthday-problem], which reveals that with just 23 people, the probability of a shared birthday exceeds 50%.

Ah. So _that's_ why Uncle Ted was so confident.

Determined to turn the tables, you start looking up other paradoxes; next time, _you'll_ be the one making the bets.

~~~~exercism/note
The birthday paradox is a [veridical paradox][veridical-paradox]: even though it feels wrong, it is actually true.
[veridical-paradox]: https://en.wikipedia.org/wiki/Paradox#Quine's_classification
~~~~

[birthday-problem]: https://en.wikipedia.org/wiki/Birthday_problem
19 changes: 19 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ryanplusplus"
],
"files": {
"solution": [
"baffling-birthdays.lua"
],
"test": [
"baffling-birthdays_spec.lua"
],
"example": [
".meta/example.lua"
]
},
"blurb": "Estimate the birthday paradox's probabilities.",
"source": "Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/pull/2539"
}
41 changes: 41 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local baffling_birthdays = {}

baffling_birthdays.shared_birthday = function(birthdates)
local seen = {}
for _, birthdate in ipairs(birthdates) do
local without_year = birthdate:sub(6)
if seen[without_year] then
return true
end
seen[without_year] = true
end
return false
end

baffling_birthdays.random_birthdates = function(count)
local birthdates = {}

local non_leap_year = 2026
local seconds_per_day = 24 * 60 * 60
local start = os.time({ year = non_leap_year, month = 1, day = 1, hour = 12 })
for i = 1, count do
local offset = math.random(0, 364)
local t = start + offset * seconds_per_day
table.insert(birthdates, os.date('%Y-%m-%d', t))
end

return birthdates
end

baffling_birthdays.estimated_probability_of_shared_birthday = function(group_size)
local shared_count = 0
for i = 1, 10000 do
local birthdates = baffling_birthdays.random_birthdates(group_size)
if baffling_birthdays.shared_birthday(birthdates) then
shared_count = shared_count + 1
end
end
return shared_count / 100
end

return baffling_birthdays
64 changes: 64 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local utils = require 'utils'

return {
module_name = 'baffling_birthdays',

generate_test = function(case)
if type(case.expected) == 'table' then
if case.expected.years and case.expected.years.leapYear ~= nil then
return [[
local birthdates = baffling_birthdays.random_birthdates(1000)
for _, birthdate in ipairs(birthdates) do
local year = tonumber(birthdate:sub(1, 4))
assert.is_false(year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0))
end]]
elseif case.expected.months and case.expected.months.random ~= nil then
return [[
local birthdates = baffling_birthdays.random_birthdates(1000)
local seen = {}
local unique_count = 0
for _, birthdate in ipairs(birthdates) do
local month = tonumber(birthdate:sub(6, 7))
if not seen[month] then
seen[month] = true
unique_count = unique_count + 1
end
end
assert.are.equal(12, unique_count)]]
elseif case.expected.days and case.expected.days.random ~= nil then
return [[
local birthdates = baffling_birthdays.random_birthdates(1000)
local seen = {}
local unique_count = 0
for _, birthdate in ipairs(birthdates) do
local day = tonumber(birthdate:sub(9, 10))
if not seen[day] then
seen[day] = true
unique_count = unique_count + 1
end
end
assert.are.equal(31, unique_count)]]
else
error('Unhandled case')
end
elseif case.expected == 'length == groupsize' then
return [[
for i = 1, 10 do
assert.are.equal(i, #baffling_birthdays.random_birthdates(i))
end]]
elseif case.property == 'estimatedProbabilityOfSharedBirthday' then
local template = [[
local actual = baffling_birthdays.%s(%s)
assert.are.near(%s, actual, 1)]]

return template:format(utils.snake_case(case.property), case.input.groupSize, case.expected)
else
local template = [[
local actual = baffling_birthdays.%s(%s)
assert.are.equal(%s, actual)]]

local input = '{ ' .. table.concat(utils.map(case.input.birthdates, utils.stringify), ', ') .. ' }'
return template:format(utils.snake_case(case.property), input, case.expected)
end
end
}
61 changes: 61 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[716dcc2b-8fe4-4fc9-8c48-cbe70d8e6b67]
description = "shared birthday -> one birthdate"

[f7b3eb26-bcfc-4c1e-a2de-af07afc33f45]
description = "shared birthday -> two birthdates with same year, month, and day"

[7193409a-6e16-4bcb-b4cc-9ffe55f79b25]
description = "shared birthday -> two birthdates with same year and month, but different day"

[d04db648-121b-4b72-93e8-d7d2dced4495]
description = "shared birthday -> two birthdates with same month and day, but different year"

[3c8bd0f0-14c6-4d4c-975a-4c636bfdc233]
description = "shared birthday -> two birthdates with same year, but different month and day"

[df5daba6-0879-4480-883c-e855c99cdaa3]
description = "shared birthday -> two birthdates with different year, month, and day"

[0c17b220-cbb9-4bd7-872f-373044c7b406]
description = "shared birthday -> multiple birthdates without shared birthday"

[966d6b0b-5c0a-4b8c-bc2d-64939ada49f8]
description = "shared birthday -> multiple birthdates with one shared birthday"

[b7937d28-403b-4500-acce-4d9fe3a9620d]
description = "shared birthday -> multiple birthdates with more than one shared birthday"

[70b38cea-d234-4697-b146-7d130cd4ee12]
description = "random birthdates -> generate requested number of birthdates"

[d9d5b7d3-5fea-4752-b9c1-3fcd176d1b03]
description = "random birthdates -> years are not leap years"

[d1074327-f68c-4c8a-b0ff-e3730d0f0521]
description = "random birthdates -> months are random"

[7df706b3-c3f5-471d-9563-23a4d0577940]
description = "random birthdates -> days are random"

[89a462a4-4265-4912-9506-fb027913f221]
description = "estimated probability of at least one shared birthday -> for one person"

[ec31c787-0ebb-4548-970c-5dcb4eadfb5f]
description = "estimated probability of at least one shared birthday -> among ten people"

[b548afac-a451-46a3-9bb0-cb1f60c48e2f]
description = "estimated probability of at least one shared birthday -> among twenty-three people"

[e43e6b9d-d77b-4f6c-a960-0fc0129a0bc5]
description = "estimated probability of at least one shared birthday -> among seventy people"
12 changes: 12 additions & 0 deletions exercises/practice/baffling-birthdays/baffling-birthdays.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local baffling_birthdays = {}

baffling_birthdays.shared_birthday = function(birthdates)
end

baffling_birthdays.random_birthdates = function(count)
end

baffling_birthdays.estimated_probability_of_shared_birthday = function(group_size)
end

return baffling_birthdays
Loading
Loading