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
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ Function Invoke-BafflingBirthdays {

.PARAMETER People
Number of people in the group.

.NOTES
Consider doing 5000 runs of the test for optimal result for 2% tolerance.
#>
[CmdletBinding()]
Param(
[int]$People
)
$runs = 1000
$runs = 5000
$count = 0
for ($i = 0; $i -lt $runs; $i++) {
$birthdays = Get-RandomBirthdates -People $People
$count += Test-SharedBirthday -Birthdates $birthdays
$count += Test-SharedBirthday -Birthdates (Get-RandomBirthdates -People $People)
}
$count / 100.00
}

Function Get-RandomBirthdate([int]$Year) {
$month = Get-Random -Minimum 1 -Maximum (12 + 1)
$day = Get-Random -Minimum 1 -Maximum (([DateTime]::DaysInMonth($Year, $month)) + 1)
[datetime]::New($Year, $month, $day)
$count * 100.00 / $runs
}

Function Get-RandomBirthdates {
Expand All @@ -47,9 +43,8 @@ Function Get-RandomBirthdates {
$year = Get-Random -Minimum 1900 -Maximum ((Get-Date).Year + 1)
} until (-not [DateTime]::IsLeapYear($year))

for ($i = 0; $i -lt $People; $i++) {
Get-RandomBirthdate $year
}
Get-Random -Minimum 0 -Maximum 365 -Count $People |
ForEach-Object { [DateTime]::new($year, 1, 1).AddDays($_) }
}

Function Test-SharedBirthday {
Expand All @@ -64,7 +59,7 @@ Function Test-SharedBirthday {
Param(
[DateTime[]]$Birthdates
)
for ($i = 0; $i -lt $Birthdates.Count; $i++) {
for ($i = 0; $i -lt $Birthdates.Count - 1; $i++) {
for ($j = $i+1; $j -lt $Birthdates.Count; $j++) {
if ($Birthdates[$i].DayOfYear -eq $Birthdates[$j].DayOfYear) {
return $true
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/baffling-birthdays/BafflingBirthdays.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Function Invoke-BafflingBirthdays {

.PARAMETER People
Number of people in the group.

.NOTES
Consider doing 5000 runs of the test for optimal result for 2% tolerance.
#>
[CmdletBinding()]
Param(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ Describe "BafflingBirthdays test cases" {

Context "estimated probability" {
BeforeAll {
$tolerance = 1
$tolerance = 2
function Test-Probability($Probability, $Expected, $Tolerance) {
($Probability -ge $Expected - $Tolerance) -or ($Probability -le $Expected + $Tolerance)
($Probability -ge $Expected - $Tolerance) -and ($Probability -le $Expected + $Tolerance)
}
}

Expand Down