Created
June 25, 2021 12:29
-
-
Save megastary/cd49ad963b0b5cc113d0e32c5fda23ec to your computer and use it in GitHub Desktop.
Making change
This file contains 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
# Author: Jakub Šindelář megastary@houby-studio.eu | |
# Date: 2021-06-25 | |
# Solution definition | |
function change { | |
param ( | |
[ValidateRange(0, [Int32]::MaxValue)] | |
[Int32]$Value, | |
[System.Array]$Coins = @(1, 5, 10, 25, 100, 500) | |
) | |
$Result = 0 | |
$RemainingChange = $Value | |
$Coins | Sort-Object -Descending | ForEach-Object { | |
$Result += [Math]::Floor([Decimal]($RemainingChange / $_)) | |
$RemainingChange = $RemainingChange % $_ | |
} | |
return $Result | |
} |
This file contains 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
# Test result with Pester | |
# Install with: Install-Module Pester -MinimumVersion 4.0.0 -Force -SkipPublisherCheck | |
# Import tested script | |
BeforeAll { | |
. $PSCommandPath.Replace('.Tests.ps1', '.ps1') | |
} | |
Describe 'change' { | |
Context '0' { | |
It 'returns 0' { | |
change(0) | Should -Be 0 | |
} | |
} | |
Context '12' { | |
It 'returns 3' { | |
change(12) | Should -Be 3 | |
} | |
} | |
Context '468' { | |
It 'returns 11' { | |
change(468) | Should -Be 11 | |
} | |
} | |
Context '123456' { | |
It 'returns 254' { | |
change(123456) | Should -Be 254 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is my PowerShell solution to challenge https://old.reddit.com/r/dailyprogrammer/comments/nucsik/20210607_challenge_393_easy_making_change/