Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add practice exercise change #754

Merged
merged 1 commit into from
Jul 16, 2024
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "change",
"name": "Change",
"uuid": "7006d489-ca27-4843-9f0b-843e94e5dbd2",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/change/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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?
17 changes: 17 additions & 0 deletions exercises/practice/change/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
18 changes: 18 additions & 0 deletions exercises/practice/change/.meta/example.jl
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions exercises/practice/change/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions exercises/practice/change/change.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function change(coins, target)
# Your code here
end
57 changes: 57 additions & 0 deletions exercises/practice/change/runtests.jl
Original file line number Diff line number Diff line change
@@ -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