Skip to content

Add change #345

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

Merged
merged 1 commit into from
Jul 9, 2025
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 @@ -635,6 +635,14 @@
"strings"
]
},
{
"slug": "change",
"name": "Change",
"uuid": "25347375-b351-4206-9f6e-bb0e28dd1fb9",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/change/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Instructions

Determine the fewest number of coins to give a customer so that the sum of their values equals the correct amount of change.

## Examples

- An amount of 15 with available coin values [1, 5, 10, 25, 100] should return one coin of value 5 and one coin of value 10, or [5, 10].
- An amount of 40 with available coin values [1, 5, 10, 25, 100] should return one coin of value 5, one coin of value 10, and one coin of value 25, or [5, 10, 25].
26 changes: 26 additions & 0 deletions exercises/practice/change/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Introduction

In the mystical village of Coinholt, you stand behind the counter of your bakery, arranging a fresh batch of pastries.
The door creaks open, and in walks Denara, a skilled merchant with a keen eye for quality goods.
After a quick meal, she slides a shimmering coin across the counter, representing a value of 100 units.

You smile, taking the coin, and glance at the total cost of the meal: 88 units.
That means you need to return 12 units in change.

Denara holds out her hand expectantly.
"Just give me the fewest coins," she says with a smile.
"My pouch is already full, and I don't want to risk losing them on the road."

You know you have a few options.
"We have Lumis (worth 10 units), Viras (worth 5 units), and Zenth (worth 2 units) available for change."

You quickly calculate the possibilities in your head:

- one Lumis (1 × 10 units) + one Zenth (1 × 2 units) = 2 coins total
- two Viras (2 × 5 units) + one Zenth (1 × 2 units) = 3 coins total
- six Zenth (6 × 2 units) = 6 coins total

"The best choice is two coins: one Lumis and one Zenth," you say, handing her the change.

Denara smiles, clearly impressed.
"As always, you've got it right."
19 changes: 19 additions & 0 deletions exercises/practice/change/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"change.vim"
],
"test": [
"change.vader"
],
"example": [
".meta/example.vim"
]
},
"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"
}
40 changes: 40 additions & 0 deletions exercises/practice/change/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function! FindFewestCoins(coins, target_balance) abort
if a:target_balance < 0
throw "target can't be negative"
endif

if a:target_balance == 0
return []
endif

let l:available_coins = sort(a:coins, {a, b -> b - a})

let l:queue = [0]
let l:visited = {0: []}

while !empty(l:queue)
let l:starting_balance = remove(l:queue, 0)

for l:coin in l:available_coins
let l:updated_balance = l:starting_balance + l:coin

if l:updated_balance > a:target_balance || has_key(l:visited, l:updated_balance)
continue
endif

let l:used_coins = copy(l:visited[l:starting_balance])
call add(l:used_coins, l:coin)
call sort(l:used_coins, {a, b -> b - a})

if l:updated_balance == a:target_balance
return sort(l:used_coins, 'n')
endif

let l:visited[l:updated_balance] = l:used_coins

call add(l:queue, l:updated_balance)
endfor
endwhile

throw "can't make target with given coins"
endfunction
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"
80 changes: 80 additions & 0 deletions exercises/practice/change/change.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Execute (change for 1 cent):
let g:coins = [1, 5, 10, 25]
let g:target = 1
let g:expected = [1]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (single coin change):
let g:coins = [1, 5, 10, 25, 100]
let g:target = 25
let g:expected = [25]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (multiple coin change):
let g:coins = [1, 5, 10, 25, 100]
let g:target = 15
let g:expected = [5, 10]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (change with Lilliputian Coins):
let g:coins = [1, 4, 15, 20, 50]
let g:target = 23
let g:expected = [4, 4, 15]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (change with Lower Elbonia Coins):
let g:coins = [1, 5, 10, 21, 25]
let g:target = 63
let g:expected = [21, 21, 21]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (large target values):
let g:coins = [1, 2, 5, 10, 20, 50, 100]
let g:target = 999
let g:expected = [2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (possible change without unit coins available):
let g:coins = [2, 5, 10, 20, 50]
let g:target = 21
let g:expected = [2, 2, 2, 5, 10]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (another possible change without unit coins available):
let g:coins = [4, 5]
let g:target = 27
let g:expected = [4, 4, 4, 5, 5, 5]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (a greedy approach is not optimal):
let g:coins = [1, 10, 11]
let g:target = 20
let g:expected = [10, 10]
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (no coins make 0 change):
let g:coins = [1, 5, 10, 21, 25]
let g:target = 0
let g:expected = []
AssertEqual g:expected, FindFewestCoins(g:coins, g:target)

Execute (error testing for change smaller than the smallest of coins):
let g:coins = [5, 10]
let g:target = 3
let g:expected = "can't make target with given coins"
AssertThrows call FindFewestCoins(g:coins, g:target)
AssertEqual g:expected, g:vader_exception

Execute (error if no combination can add up to target):
let g:coins = [5, 10]
let g:target = 94
let g:expected = "can't make target with given coins"
AssertThrows call FindFewestCoins(g:coins, g:target)
AssertEqual g:expected, g:vader_exception

Execute (cannot find negative change values):
let g:coins = [1, 2, 5]
let g:target = -5
let g:expected = "target can't be negative"
AssertThrows call FindFewestCoins(g:coins, g:target)
AssertEqual g:expected, g:vader_exception
6 changes: 6 additions & 0 deletions exercises/practice/change/change.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"
" Return the fewest number of available coins needed to equal the target.
"
function! FindFewestCoins(coins, target) abort
" your solution goes here
endfunction