Skip to content
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

Add baffling-birthdays exercise #2402

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,14 @@
"for-loops"
],
"difficulty": 7
},
{
"slug": "baffling-birthdays",
"name": "baffling-birthdays",
"uuid": "f75d1dd2-63ee-4258-a4e9-f208fb8e8bd4",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
],
"foregone": [
Expand Down
7 changes: 7 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateOfTicTacToe", "practic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BottleSong", "practice\bottle-song\BottleSong.csproj", "{6BD384E6-225E-4F8A-856C-3079957C6E36}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BafflingBirthdays", "practice\baffling-birthdays\BafflingBirthdays.csproj", "{9D596842-830C-417D-807A-6B910EE467B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1063,6 +1065,10 @@ Global
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6BD384E6-225E-4F8A-856C-3079957C6E36}.Release|Any CPU.Build.0 = Release|Any CPU
{9D596842-830C-417D-807A-6B910EE467B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D596842-830C-417D-807A-6B910EE467B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D596842-830C-417D-807A-6B910EE467B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D596842-830C-417D-807A-6B910EE467B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1243,6 +1249,7 @@ Global
{8E276065-1371-4CFA-BA20-95225EC6AEBC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{67E9BAB3-9805-42F1-9298-E9BBB795140E} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{6BD384E6-225E-4F8A-856C-3079957C6E36} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{9D596842-830C-417D-807A-6B910EE467B4} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Expand Down
15 changes: 15 additions & 0 deletions exercises/practice/baffling-birthdays/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Instructions

Your task is to implement a solution that verifies the Birthday Problem's probabilities.

To do this, you need to:

- Determine if two birthdates match (same month and day).
- Generate random birthdates.
- Check if a set of randomly generated birthdates contains at least one matching pair.
- Calculate the probability of at least one match for different group sizes.

```exercism/caution
The Birthday Problem assumes that birthdays are uniformly distributed within a single year.
While the year should vary between different random birthdate generation calls, all birthdates within a single call must share the same year.
```
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 graduation, 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 365 possible birthdays, so you confidently accept.

To your astonishment, after collecting just 32 birthdays, you've already found a match.
Magnanimous, 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 Problem][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: 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
141 changes: 141 additions & 0 deletions exercises/practice/baffling-birthdays/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

root = true

[*]
indent_style = space

[BafflingBirthdays.cs]
indent_size = 4

###############################
# .NET Coding Conventions #
###############################

# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
Empty file.
17 changes: 17 additions & 0 deletions exercises/practice/baffling-birthdays/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"BafflingBirthdays.cs"
],
"test": [
"BafflingBirthdaysTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"blurb": ""
}
34 changes: 34 additions & 0 deletions exercises/practice/baffling-birthdays/BafflingBirthdays.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public static class BafflingBirthdays
{
public static bool MatchingBirthday(DateOnly birthday1, DateOnly birthday2) =>
birthday1.Month == birthday2.Month && birthday1.Day == birthday2.Day;

private static DateOnly RandomBirthdate(int year)
{
var month = Random.Shared.Next(1, 12 + 1);
var day = Random.Shared.Next(1, DateTime.DaysInMonth(year, month) + 1);
return new DateOnly(year, month, day);
}

public static DateOnly[] RandomBirthdates(int numberOfBirthdays)
{
var year = Random.Shared.Next(1900, DateTime.Now.Year + 1);
return Enumerable.Range(0, numberOfBirthdays).Select(_ => RandomBirthdate(year)).ToArray();
}

public static bool HasMatchingBirthdays(DateOnly[] birthdays) =>
birthdays
.SelectMany((birthday, i) => birthdays.Skip(i + 1), MatchingBirthday)
.Any(matching => matching);

public static double LikelihoodHasMatchingBirthday(int numberOfBirthdays)
{
var matchingBirthdayIterationCount = 0;
for (var i = 0; i < 10000; i++)
{
if (HasMatchingBirthdays(RandomBirthdates(numberOfBirthdays)))
matchingBirthdayIterationCount++;
}
return matchingBirthdayIterationCount / 100.0;
}
}
16 changes: 16 additions & 0 deletions exercises/practice/baffling-birthdays/BafflingBirthdays.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.v3" Version="1.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" />
</ItemGroup>
</Project>
Loading
Loading