diff --git a/config.json b/config.json index 484d807d0..92dfd05b1 100644 --- a/config.json +++ b/config.json @@ -1211,6 +1211,20 @@ ], "difficulty": 6 }, + { + "slug": "baffling-birthdays", + "name": "Baffling Birthdays", + "uuid": "b534049a-5920-4906-9091-0fa6d81a3636", + "practices": [], + "prerequisites": [ + "for-loops", + "lists", + "sets", + "randomness", + "datetime" + ], + "difficulty": 6 + }, { "slug": "bank-account", "name": "Bank Account", diff --git a/exercises/practice/baffling-birthdays/.docs/instructions.md b/exercises/practice/baffling-birthdays/.docs/instructions.md new file mode 100644 index 000000000..a01ec8679 --- /dev/null +++ b/exercises/practice/baffling-birthdays/.docs/instructions.md @@ -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. +~~~~ diff --git a/exercises/practice/baffling-birthdays/.docs/introduction.md b/exercises/practice/baffling-birthdays/.docs/introduction.md new file mode 100644 index 000000000..97dabd1e6 --- /dev/null +++ b/exercises/practice/baffling-birthdays/.docs/introduction.md @@ -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 diff --git a/exercises/practice/baffling-birthdays/.meta/config.json b/exercises/practice/baffling-birthdays/.meta/config.json new file mode 100644 index 000000000..6e401cb62 --- /dev/null +++ b/exercises/practice/baffling-birthdays/.meta/config.json @@ -0,0 +1,22 @@ +{ + "authors": [ + "Baboushka" + ], + "files": { + "solution": [ + "src/main/java/BafflingBirthdays.java" + ], + "test": [ + "src/test/java/BafflingBirthdaysTest.java" + ], + "example": [ + ".meta/src/reference/java/BafflingBirthdays.java" + ], + "invalidator": [ + "build.gradle" + ] + }, + "blurb": "Estimate the birthday paradox's probabilities.", + "source": "Erik Schierboom", + "source_url": "https://github.com/exercism/problem-specifications/pull/2539" +} diff --git a/exercises/practice/baffling-birthdays/.meta/src/reference/java/BafflingBirthdays.java b/exercises/practice/baffling-birthdays/.meta/src/reference/java/BafflingBirthdays.java new file mode 100644 index 000000000..03cf1ae9a --- /dev/null +++ b/exercises/practice/baffling-birthdays/.meta/src/reference/java/BafflingBirthdays.java @@ -0,0 +1,50 @@ +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.concurrent.ThreadLocalRandom; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +class BafflingBirthdays { + private static final int nonLeapYear = 2001; + private static final int daysInYear = 365; + + boolean sharedBirthday(List birthdates) { + Set seen = new HashSet<>(); + for (String birthdate : birthdates) { + String monthDay = birthdate.substring(5); + if (!seen.add(monthDay)) { + return true; + } + } + return false; + } + + List randomBirthdates(int groupSize) { + if (groupSize <= 0) { + return List.of(); + } + List birthdates = new ArrayList<>(groupSize); + ThreadLocalRandom random = ThreadLocalRandom.current(); + for (int i = 0; i < groupSize; i++) { + int dayOfYear = random.nextInt(1, daysInYear + 1); + LocalDate date = LocalDate.ofYearDay(nonLeapYear, dayOfYear); + birthdates.add(date.toString()); + } + return birthdates; + } + + double estimatedProbabilityOfSharedBirthday(int groupSize) { + if (groupSize <= 1) { + return 0.0; + } + if (groupSize > daysInYear) { + return 100.0; + } + double probabilityNoSharedBirthday = 1.0; + for (int k = 0; k < groupSize; k++) { + probabilityNoSharedBirthday *= (daysInYear - k) / (double) daysInYear; + } + return (1 - probabilityNoSharedBirthday) * 100.0; + } +} diff --git a/exercises/practice/baffling-birthdays/.meta/tests.toml b/exercises/practice/baffling-birthdays/.meta/tests.toml new file mode 100644 index 000000000..c76afb466 --- /dev/null +++ b/exercises/practice/baffling-birthdays/.meta/tests.toml @@ -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" diff --git a/exercises/practice/baffling-birthdays/build.gradle b/exercises/practice/baffling-birthdays/build.gradle new file mode 100644 index 000000000..d28f35dee --- /dev/null +++ b/exercises/practice/baffling-birthdays/build.gradle @@ -0,0 +1,25 @@ +plugins { + id "java" +} + +repositories { + mavenCentral() +} + +dependencies { + testImplementation platform("org.junit:junit-bom:5.10.0") + testImplementation "org.junit.jupiter:junit-jupiter" + testImplementation "org.assertj:assertj-core:3.25.1" + + testRuntimeOnly "org.junit.platform:junit-platform-launcher" +} + +test { + useJUnitPlatform() + + testLogging { + exceptionFormat = "full" + showStandardStreams = true + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.jar b/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..f8e1ee312 Binary files /dev/null and b/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.jar differ diff --git a/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.properties b/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..4d97ea344 --- /dev/null +++ b/exercises/practice/baffling-birthdays/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/exercises/practice/baffling-birthdays/gradlew b/exercises/practice/baffling-birthdays/gradlew new file mode 100644 index 000000000..adff685a0 --- /dev/null +++ b/exercises/practice/baffling-birthdays/gradlew @@ -0,0 +1,248 @@ +#!/bin/sh + +# +# Copyright © 2015 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/exercises/practice/baffling-birthdays/gradlew.bat b/exercises/practice/baffling-birthdays/gradlew.bat new file mode 100644 index 000000000..c4bdd3ab8 --- /dev/null +++ b/exercises/practice/baffling-birthdays/gradlew.bat @@ -0,0 +1,93 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/exercises/practice/baffling-birthdays/src/main/java/BafflingBirthdays.java b/exercises/practice/baffling-birthdays/src/main/java/BafflingBirthdays.java new file mode 100644 index 000000000..273c6506c --- /dev/null +++ b/exercises/practice/baffling-birthdays/src/main/java/BafflingBirthdays.java @@ -0,0 +1,15 @@ +import java.util.List; + +class BafflingBirthdays { + boolean sharedBirthday(List birthdates) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + List randomBirthdates(int groupSize) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double estimatedProbabilityOfSharedBirthday(int groupSize) { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } +} \ No newline at end of file diff --git a/exercises/practice/baffling-birthdays/src/test/java/BafflingBirthdaysTest.java b/exercises/practice/baffling-birthdays/src/test/java/BafflingBirthdaysTest.java new file mode 100644 index 000000000..9c9497a92 --- /dev/null +++ b/exercises/practice/baffling-birthdays/src/test/java/BafflingBirthdaysTest.java @@ -0,0 +1,160 @@ +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.data.Offset.offset; + +import java.time.LocalDate; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class BafflingBirthdaysTest { + private BafflingBirthdays birthdays = new BafflingBirthdays(); + + @Test + @DisplayName("one birthdate") + public void oneBirthdateTest() { + assertThat(birthdays.sharedBirthday(List.of("2000-01-01"))).isFalse(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("two birthdates with same year, month, and day") + public void twoBirthdatesWithSameYearMonthAndDayTest() { + assertThat(birthdays.sharedBirthday(List.of("2000-01-01", "2000-01-01"))).isTrue(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("two birthdates with same year and month, but different day") + public void twoBirthdatesWithSameYearAndMonthButDifferentDayTest() { + assertThat(birthdays.sharedBirthday(List.of("2012-05-09", "2012-05-17"))).isFalse(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("two birthdates with same month and day, but different year") + public void twoBirthdatesWithSameMonthAndDayButDifferentYearTest() { + assertThat(birthdays.sharedBirthday(List.of("1999-10-23", "1988-10-23"))).isTrue(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("two birthdates with same year, but different month and day") + public void twoBirthdatesWithSameYearButDifferentMonthAndDayTest() { + assertThat(birthdays.sharedBirthday(List.of("2007-12-19", "2007-04-27"))).isFalse(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("two birthdates with different year, month, and day") + public void twoBirthdatesWithDifferentYearMonthAndDayTest() { + assertThat(birthdays.sharedBirthday(List.of("1997-08-04", "1963-11-23"))).isFalse(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("multiple birthdates without shared birthday") + public void multipleBirthdatesWithoutSharedBirthdayTest() { + assertThat(birthdays.sharedBirthday(List.of("1966-07-29", "1977-02-12", "2001-12-25", "1980-11-10"))).isFalse(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("multiple birthdates with one shared birthday") + public void multipleBirthdatesWithOneSharedBirthdayTest() { + assertThat(birthdays.sharedBirthday(List.of("1966-07-29", "1977-02-12", "2001-07-29", "1980-11-10"))).isTrue(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("multiple birthdates with more than one shared birthday") + public void multipleBirthdatesWithMoreThanOneSharedBirthdayTest() { + assertThat(birthdays.sharedBirthday(List.of( + "1966-07-29", + "1977-02-12", + "2001-12-25", + "1980-07-29", + "2019-02-12" + ))).isTrue(); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("generate requested number of birthdates") + public void generateRequestedNumberOfBirthdatesTest() { + int groupSize = 50; + assertThat(birthdays.randomBirthdates(groupSize).size()).isEqualTo(groupSize); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("years are not leap years") + public void yearsAreNotLeapYearsTest() { + for (String birthdate : birthdays.randomBirthdates(100)) { + LocalDate date = LocalDate.parse(birthdate); + assertThat(date.isLeapYear()).isFalse(); + assertThat(date.getMonthValue() == 2 && date.getDayOfMonth() == 29).isFalse(); + } + } + + @Disabled("Remove to run test") + @Test + @DisplayName("months are random") + public void monthsAreRandomTest() { + Set months = new HashSet<>(); + for (String birthdate : birthdays.randomBirthdates(500)) { + LocalDate date = LocalDate.parse(birthdate); + months.add(date.getMonthValue()); + if (months.size() >= 7) { + break; + } + } + assertThat(months).hasSizeGreaterThanOrEqualTo(7); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("days are random") + public void daysAreRandomTest() { + Set days = new HashSet<>(); + for (String birthdate : birthdays.randomBirthdates(500)) { + LocalDate date = LocalDate.parse(birthdate); + days.add(date.getDayOfMonth()); + if (days.size() >= 11) { + break; + } + } + assertThat(days).hasSizeGreaterThanOrEqualTo(11); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("estimated probability of at least one shared birthday case for one person") + public void estimatedProbabilityOfAtLeastOneSharedBirthdayForOnePersonTest() { + assertThat(birthdays.estimatedProbabilityOfSharedBirthday(1)).isEqualTo(0.0); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("estimated probability of at least one shared birthday case among ten people") + public void estimatedProbabilityOfAtLeastOneSharedBirthdayAmongTenPeopleTest() { + assertThat(birthdays.estimatedProbabilityOfSharedBirthday(10)).isCloseTo(11.694818, offset(1.0)); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("estimated probability of at least one shared birthday case among twenty-three people") + public void estimatedProbabilityOfAtLeastOneSharedBirthdayAmongTwentyThreePeopleTest() { + assertThat(birthdays.estimatedProbabilityOfSharedBirthday(23)).isCloseTo(50.729723, offset(1.0)); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("estimated probability of at least one shared birthday case among seventy people") + public void estimatedProbabilityOfAtLeastOneSharedBirthdayAmongSeventyPeopleTest() { + assertThat(birthdays.estimatedProbabilityOfSharedBirthday(70)).isCloseTo(99.915958, offset(1.0)); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index 1119f4b98..6bf710357 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -40,6 +40,7 @@ include 'practice:bank-account' // include 'practice:binary' // deprecated include 'practice:binary-search' include 'practice:binary-search-tree' +include 'practice:baffling-birthdays' include 'practice:bob' include 'practice:book-store' include 'practice:bottle-song'