Skip to content

Instantly share code, notes, and snippets.

@megastary
Created June 25, 2021 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save megastary/cd49ad963b0b5cc113d0e32c5fda23ec to your computer and use it in GitHub Desktop.
Save megastary/cd49ad963b0b5cc113d0e32c5fda23ec to your computer and use it in GitHub Desktop.
Making change
# 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
}
# 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
}
}
}
@megastary
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment