diff --git a/config.json b/config.json index 0b7b4f63..a0379614 100644 --- a/config.json +++ b/config.json @@ -961,6 +961,14 @@ "practices": [], "prerequisites": [], "difficulty": 5 + }, + { + "slug": "change", + "name": "Change", + "uuid": "7006d489-ca27-4843-9f0b-843e94e5dbd2", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, diff --git a/exercises/practice/change/.docs/instructions.md b/exercises/practice/change/.docs/instructions.md new file mode 100644 index 00000000..30fa5677 --- /dev/null +++ b/exercises/practice/change/.docs/instructions.md @@ -0,0 +1,14 @@ +# Instructions + +Correctly determine the fewest number of coins to be given to a customer such that the sum of the coins' value would equal the correct amount of change. + +## For example + +- An input of 15 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) or [5, 10] +- An input of 40 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) and one quarter (25) or [5, 10, 25] + +## Edge cases + +- Does your algorithm work for any given set of coins? +- Can you ask for negative change? +- Can you ask for a change value smaller than the smallest coin value? diff --git a/exercises/practice/change/.meta/config.json b/exercises/practice/change/.meta/config.json new file mode 100644 index 00000000..eab0004b --- /dev/null +++ b/exercises/practice/change/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [], + "files": { + "solution": [ + "change.jl" + ], + "test": [ + "runtests.jl" + ], + "example": [ + ".meta/example.jl" + ] + }, + "blurb": "Correctly determine change to be given using the least number of coins.", + "source": "Software Craftsmanship - Coin Change Kata", + "source_url": "https://web.archive.org/web/20130115115225/http://craftsmanship.sv.cmu.edu:80/exercises/coin-change-kata" +} diff --git a/exercises/practice/change/.meta/example.jl b/exercises/practice/change/.meta/example.jl new file mode 100644 index 00000000..ccf709b5 --- /dev/null +++ b/exercises/practice/change/.meta/example.jl @@ -0,0 +1,18 @@ +function change(coins, target) + memo = Dict(0 => (0, 0)) + for n in 1:target + mincoins, previousn = minimum([(first(get(memo, prevn, (Inf, 0))), prevn) for prevn in n .- coins]) + mincoins < Inf && (memo[n] = (mincoins+1, previousn)) + end + + !haskey(memo, target) && throw(DomainError(target, "Not Possible")) + + changeback, previoustarget = [], Inf + while 0 < target + previoustarget = last(memo[target]) + push!(changeback, target - previoustarget) + target = previoustarget + end + + reverse!(changeback) +end diff --git a/exercises/practice/change/.meta/tests.toml b/exercises/practice/change/.meta/tests.toml new file mode 100644 index 00000000..2d2f44bc --- /dev/null +++ b/exercises/practice/change/.meta/tests.toml @@ -0,0 +1,49 @@ +# 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. + +[d0ebd0e1-9d27-4609-a654-df5c0ba1d83a] +description = "change for 1 cent" + +[36887bea-7f92-4a9c-b0cc-c0e886b3ecc8] +description = "single coin change" + +[cef21ccc-0811-4e6e-af44-f011e7eab6c6] +description = "multiple coin change" + +[d60952bc-0c1a-4571-bf0c-41be72690cb3] +description = "change with Lilliputian Coins" + +[408390b9-fafa-4bb9-b608-ffe6036edb6c] +description = "change with Lower Elbonia Coins" + +[7421a4cb-1c48-4bf9-99c7-7f049689132f] +description = "large target values" + +[f79d2e9b-0ae3-4d6a-bb58-dc978b0dba28] +description = "possible change without unit coins available" + +[9a166411-d35d-4f7f-a007-6724ac266178] +description = "another possible change without unit coins available" + +[ce0f80d5-51c3-469d-818c-3e69dbd25f75] +description = "a greedy approach is not optimal" + +[bbbcc154-e9e9-4209-a4db-dd6d81ec26bb] +description = "no coins make 0 change" + +[c8b81d5a-49bd-4b61-af73-8ee5383a2ce1] +description = "error testing for change smaller than the smallest of coins" + +[3c43e3e4-63f9-46ac-9476-a67516e98f68] +description = "error if no combination can add up to target" + +[8fe1f076-9b2d-4f44-89fe-8a6ccd63c8f3] +description = "cannot find negative change values" diff --git a/exercises/practice/change/change.jl b/exercises/practice/change/change.jl new file mode 100644 index 00000000..182658f8 --- /dev/null +++ b/exercises/practice/change/change.jl @@ -0,0 +1,3 @@ +function change(coins, target) + # Your code here +end diff --git a/exercises/practice/change/runtests.jl b/exercises/practice/change/runtests.jl new file mode 100644 index 00000000..dda1233b --- /dev/null +++ b/exercises/practice/change/runtests.jl @@ -0,0 +1,57 @@ +using Test + +include("change.jl") + +@testset verbose = true "tests" begin + @testset "change for 1 cent" begin + @test change([1, 5, 10, 25], 1) == [1] + end + + @testset "single coin change" begin + @test change([1, 5, 10, 25, 100], 25) == [25] + end + + @testset "multiple coin change" begin + @test change([1, 5, 10, 25, 100], 15) == [5, 10] + end + + @testset "change with Lilliputian Coins" begin + @test change([1, 4, 15, 20, 50], 23) == [4, 4, 15] + end + + @testset "change with Lower Elbonia Coins" begin + @test change([1, 5, 10, 21, 25], 63) == [21, 21, 21] + end + + @testset "large target values" begin + @test change([1, 2, 5, 10, 20, 50, 100], 999) == [2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100] + end + + @testset "possible change without unit coins available" begin + @test change([2, 5, 10, 20, 50], 21) == [2, 2, 2, 5, 10] + end + + @testset "another possible change without unit coins available" begin + @test change([4, 5], 27) == [4, 4, 4, 5, 5, 5] + end + + @testset "a greedy approach is not optimal" begin + @test change([1, 10, 11], 20) == [10, 10] + end + + @testset "no coins make 0 change" begin + @test change([1, 5, 10, 21, 25], 0) == [] + end + + @testset "error testing for change smaller than the smallest of coins" begin + @test_throws DomainError change([5, 10], 3) + end + + @testset "error if no combination can add up to target" begin + @test_throws DomainError change([5, 10], 94) + end + + @testset "cannot find negative change values" begin + @test_throws DomainError change([1, 2, 5], -5) + end +end