-
-
Notifications
You must be signed in to change notification settings - Fork 410
Add new practice exercise baffling-birthdays
#1575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
130e0a6
Add new practice exercise `baffling-birthdays`
jiegillet f1a67b6
remove unused variables
jiegillet 208ff67
use map_size
jiegillet 7c12532
fix failure rate for days
jiegillet e78a151
add prerequisites
jiegillet 42fde39
actually assert result is true
jiegillet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
exercises/practice/baffling-birthdays/.docs/instructions.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
25
exercises/practice/baffling-birthdays/.docs/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Used by "mix format" | ||
| [ | ||
| inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # The directory Mix will write compiled artifacts to. | ||
| /_build/ | ||
|
|
||
| # If you run "mix test --cover", coverage assets end up here. | ||
| /cover/ | ||
|
|
||
| # The directory Mix downloads your dependencies sources to. | ||
| /deps/ | ||
|
|
||
| # Where third-party dependencies like ExDoc output generated docs. | ||
| /doc/ | ||
|
|
||
| # Ignore .fetch files in case you like to edit your project deps locally. | ||
| /.fetch | ||
|
|
||
| # If the VM crashes, it generates a dump, let's ignore it too. | ||
| erl_crash.dump | ||
|
|
||
| # Also ignore archive artifacts (built via "mix archive.build"). | ||
| *.ez | ||
|
|
||
| # Ignore package tarball (built via "mix hex.build"). | ||
| baffling_birthdays-*.tar | ||
|
|
||
| # Temporary files, for example, from tests. | ||
| /tmp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "authors": [ | ||
| "jiegillet" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "lib/baffling_birthdays.ex" | ||
| ], | ||
| "test": [ | ||
| "test/baffling_birthdays_test.exs" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.ex" | ||
| ] | ||
| }, | ||
| "blurb": "Estimate the birthday paradox's probabilities.", | ||
| "source": "Erik Schierboom", | ||
| "source_url": "https://github.com/exercism/problem-specifications/pull/2539" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| defmodule BafflingBirthdays do | ||
| @moduledoc """ | ||
| Estimate the probability of shared birthdays in a group of people. | ||
| """ | ||
|
|
||
| @spec shared_birthday?(birthdates :: [Date.t()]) :: boolean() | ||
| def shared_birthday?(birthdates) do | ||
| result = | ||
| Enum.reduce_while(birthdates, MapSet.new(), fn date, birthdays -> | ||
| birthday = {date.month, date.day} | ||
|
|
||
| if MapSet.member?(birthdays, birthday) do | ||
| {:halt, :found_a_shared_birthday} | ||
| else | ||
| {:cont, MapSet.put(birthdays, birthday)} | ||
| end | ||
| end) | ||
|
|
||
| result == :found_a_shared_birthday | ||
| end | ||
|
|
||
| @spec random_birthdates(group_size :: integer()) :: [Date.t()] | ||
| def random_birthdates(group_size) do | ||
| for _ <- 1..group_size do | ||
| year = generate_non_leap_year_january_first(0, 3000) | ||
| days_to_add = Enum.random(0..364) | ||
| Date.add(year, days_to_add) | ||
| end | ||
| end | ||
|
|
||
| defp generate_non_leap_year_january_first(min, max) do | ||
| year = Enum.random(min..max) | ||
| january_first = Date.new!(year, 1, 1) | ||
|
|
||
| if Date.leap_year?(january_first) do | ||
| Date.add(january_first, 366) | ||
| else | ||
| january_first | ||
| end | ||
| end | ||
|
|
||
| @spec estimated_probability_of_shared_birthday(group_size :: integer()) :: float() | ||
| def estimated_probability_of_shared_birthday(group_size) do | ||
| sample_size = 1000 | ||
|
|
||
| shared_birthdays_count = | ||
| for _ <- 1..sample_size, | ||
| group_size |> random_birthdates() |> shared_birthday?(), | ||
| reduce: 0 do | ||
| count -> count + 1 | ||
| end | ||
|
|
||
| 100 * shared_birthdays_count / sample_size | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
17 changes: 17 additions & 0 deletions
17
exercises/practice/baffling-birthdays/lib/baffling_birthdays.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| defmodule BafflingBirthdays do | ||
| @moduledoc """ | ||
| Estimate the probability of shared birthdays in a group of people. | ||
| """ | ||
|
|
||
| @spec shared_birthday?(birthdates :: [Date.t()]) :: boolean() | ||
| def shared_birthday?(birthdates) do | ||
| end | ||
|
|
||
| @spec random_birthdates(group_size :: integer()) :: [Date.t()] | ||
| def random_birthdates(group_size) do | ||
| end | ||
|
|
||
| @spec estimated_probability_of_shared_birthday(group_size :: integer()) :: float() | ||
| def estimated_probability_of_shared_birthday(group_size) do | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| defmodule BafflingBirthdays.MixProject do | ||
| use Mix.Project | ||
|
|
||
| def project do | ||
| [ | ||
| app: :baffling_birthdays, | ||
| version: "0.1.0", | ||
| start_permanent: Mix.env() == :prod, | ||
| deps: deps() | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help compile.app" to learn about applications. | ||
| def application do | ||
| [ | ||
| extra_applications: [:logger] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help deps" to learn about dependencies. | ||
| defp deps do | ||
| [ | ||
| # {:dep_from_hexpm, "~> 0.3.0"}, | ||
| # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} | ||
| ] | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an interesting year range 😬 just so that we're clear: it's completely unnecessary to make the year random, right? To pass the tests, you might just as well choose the same non-leap year for all birthdates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, as far as the tests are concerned you could pick
year = 2025and be done with it, that's true.In terms of generating random
Date.t(), this felt more natural to me. I guess it doesn't really matter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right - It does not matter! I just wanted to make sure 🙂