Skip to content

Commit 06802cd

Browse files
Add change exercise (#205)
* Add change exercise * Fix
1 parent 5d8985b commit 06802cd

File tree

9 files changed

+252
-0
lines changed

9 files changed

+252
-0
lines changed

config.json

+15
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,21 @@
12241224
],
12251225
"difficulty": 7
12261226
},
1227+
{
1228+
"slug": "change",
1229+
"name": "Change",
1230+
"uuid": "4b4197d2-8123-4474-8c32-1191c1f9b5cd",
1231+
"practices": [
1232+
"exceptions",
1233+
"linq"
1234+
],
1235+
"prerequisites": [
1236+
"numbers",
1237+
"arrays",
1238+
"exceptions"
1239+
],
1240+
"difficulty": 6
1241+
},
12271242
{
12281243
"slug": "roman-numerals",
12291244
"name": "Roman Numerals",

exercises/Exercises.sln

+7
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "DndCharacter", "practice\dn
161161
EndProject
162162
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Poker", "practice\poker\Poker.vbproj", "{F62F286B-2E13-4914-B168-EDB399470B1C}"
163163
EndProject
164+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Change", "practice\change\Change.vbproj", "{D20F0E28-4936-4FA6-ADBF-E460C5043989}"
165+
EndProject
164166
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "RomanNumerals", "practice\roman-numerals\RomanNumerals.vbproj", "{C099D994-E586-4A2F-BDBF-A1D6742B1794}"
165167
EndProject
166168
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "RobotSimulator", "practice\robot-simulator\RobotSimulator.vbproj", "{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E}"
@@ -496,6 +498,10 @@ Global
496498
{F62F286B-2E13-4914-B168-EDB399470B1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
497499
{F62F286B-2E13-4914-B168-EDB399470B1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
498500
{F62F286B-2E13-4914-B168-EDB399470B1C}.Release|Any CPU.Build.0 = Release|Any CPU
501+
{D20F0E28-4936-4FA6-ADBF-E460C5043989}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
502+
{D20F0E28-4936-4FA6-ADBF-E460C5043989}.Debug|Any CPU.Build.0 = Debug|Any CPU
503+
{D20F0E28-4936-4FA6-ADBF-E460C5043989}.Release|Any CPU.ActiveCfg = Release|Any CPU
504+
{D20F0E28-4936-4FA6-ADBF-E460C5043989}.Release|Any CPU.Build.0 = Release|Any CPU
499505
{C099D994-E586-4A2F-BDBF-A1D6742B1794}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
500506
{C099D994-E586-4A2F-BDBF-A1D6742B1794}.Debug|Any CPU.Build.0 = Debug|Any CPU
501507
{C099D994-E586-4A2F-BDBF-A1D6742B1794}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -604,6 +610,7 @@ Global
604610
{057DDB74-6435-4B9E-988A-A21895AECE92} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
605611
{1C5BEE1E-B2B8-4D08-BC42-D83121DFFFB9} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
606612
{F62F286B-2E13-4914-B168-EDB399470B1C} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
613+
{D20F0E28-4936-4FA6-ADBF-E460C5043989} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
607614
{C099D994-E586-4A2F-BDBF-A1D6742B1794} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
608615
{86A4A18F-6FC1-4953-9AAE-EEEE36FE8E2E} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
609616
{1CE933B5-94BD-48AD-81B9-E352D75020CB} = {B161412A-37BB-42B7-9D8F-F4B238E3CFFA}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions
2+
3+
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.
4+
5+
## For example
6+
7+
- An input of 15 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) or [5, 10]
8+
- 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]
9+
10+
## Edge cases
11+
12+
- Does your algorithm work for any given set of coins?
13+
- Can you ask for negative change?
14+
- Can you ask for a change value smaller than the smallest coin value?
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Imports System
2+
Imports System.Collections.Generic
3+
Imports System.Linq
4+
5+
Public Module Change
6+
Public Function FindFewestCoins(ByVal coins As Integer(), ByVal target As Integer) As Integer()
7+
If target = 0 Then
8+
Return New List(Of Integer)().ToArray()
9+
End If
10+
11+
If target < 0 OrElse target < coins.Min() Then
12+
Throw New ArgumentException()
13+
End If
14+
15+
Dim fewestCoins = New Integer(target + 1 - 1)() {}
16+
fewestCoins(0) = Array.Empty(Of Integer)()
17+
18+
For amount = 1 To target
19+
Dim change As IEnumerable(Of Integer) = coins.Where(Function(coin) coin <= amount).[Select](Function(coin) fewestCoins(amount - coin).Append(coin)).OrderBy(Function(x) x.Count()).FirstOrDefault(Function(y) y.Sum() = amount)
20+
21+
If change Is Nothing Then
22+
fewestCoins(amount) = New List(Of Integer)().ToArray()
23+
Else
24+
fewestCoins(amount) = change.OrderBy(Function(coin) coin).ToArray()
25+
End If
26+
Next
27+
28+
If fewestCoins(target).Count() > 0 Then
29+
Return fewestCoins(target)
30+
End If
31+
32+
Throw New ArgumentException()
33+
End Function
34+
End Module
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"Change.vb"
8+
],
9+
"test": [
10+
"ChangeTests.vb"
11+
],
12+
"example": [
13+
".meta/Example.vb"
14+
]
15+
},
16+
"blurb": "Correctly determine change to be given using the least number of coins.",
17+
"source": "Software Craftsmanship - Coin Change Kata",
18+
"source_url": "https://web.archive.org/web/20130115115225/http://craftsmanship.sv.cmu.edu:80/exercises/coin-change-kata"
19+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[d0ebd0e1-9d27-4609-a654-df5c0ba1d83a]
13+
description = "change for 1 cent"
14+
15+
[36887bea-7f92-4a9c-b0cc-c0e886b3ecc8]
16+
description = "single coin change"
17+
18+
[cef21ccc-0811-4e6e-af44-f011e7eab6c6]
19+
description = "multiple coin change"
20+
21+
[d60952bc-0c1a-4571-bf0c-41be72690cb3]
22+
description = "change with Lilliputian Coins"
23+
24+
[408390b9-fafa-4bb9-b608-ffe6036edb6c]
25+
description = "change with Lower Elbonia Coins"
26+
27+
[7421a4cb-1c48-4bf9-99c7-7f049689132f]
28+
description = "large target values"
29+
30+
[f79d2e9b-0ae3-4d6a-bb58-dc978b0dba28]
31+
description = "possible change without unit coins available"
32+
33+
[9a166411-d35d-4f7f-a007-6724ac266178]
34+
description = "another possible change without unit coins available"
35+
36+
[bbbcc154-e9e9-4209-a4db-dd6d81ec26bb]
37+
description = "no coins make 0 change"
38+
39+
[c8b81d5a-49bd-4b61-af73-8ee5383a2ce1]
40+
description = "error testing for change smaller than the smallest of coins"
41+
42+
[3c43e3e4-63f9-46ac-9476-a67516e98f68]
43+
description = "error if no combination can add up to target"
44+
45+
[8fe1f076-9b2d-4f44-89fe-8a6ccd63c8f3]
46+
description = "cannot find negative change values"

exercises/practice/change/Change.vb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Imports System
2+
3+
Public Module Change
4+
Public Function FindFewestCoins(ByVal coins As Integer(), ByVal target As Integer) As Integer()
5+
Throw New NotImplementedException("You need to implement this function.")
6+
End Function
7+
End Module
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net7.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
8+
<PackageReference Include="xunit" Version="2.4.2" />
9+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
10+
<PrivateAssets>all</PrivateAssets>
11+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
12+
</PackageReference>
13+
</ItemGroup>
14+
</Project>
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Imports System
2+
Imports Xunit
3+
4+
Public Class ChangeTests
5+
<Fact>
6+
Public Sub Change_for_1_cent()
7+
Dim coins = {1, 5, 10, 25}
8+
Dim target = 1
9+
Dim expected = {1}
10+
Assert.Equal(expected, FindFewestCoins(coins, target))
11+
End Sub
12+
13+
<Fact(Skip:="Remove this Skip property to run this test")>
14+
Public Sub Single_coin_change()
15+
Dim coins = {1, 5, 10, 25, 100}
16+
Dim target = 25
17+
Dim expected = {25}
18+
Assert.Equal(expected, FindFewestCoins(coins, target))
19+
End Sub
20+
21+
<Fact(Skip:="Remove this Skip property to run this test")>
22+
Public Sub Multiple_coin_change()
23+
Dim coins = {1, 5, 10, 25, 100}
24+
Dim target = 15
25+
Dim expected = {5, 10}
26+
Assert.Equal(expected, FindFewestCoins(coins, target))
27+
End Sub
28+
29+
<Fact(Skip:="Remove this Skip property to run this test")>
30+
Public Sub Change_with_lilliputian_coins()
31+
Dim coins = {1, 4, 15, 20, 50}
32+
Dim target = 23
33+
Dim expected = {4, 4, 15}
34+
Assert.Equal(expected, FindFewestCoins(coins, target))
35+
End Sub
36+
37+
<Fact(Skip:="Remove this Skip property to run this test")>
38+
Public Sub Change_with_lower_elbonia_coins()
39+
Dim coins = {1, 5, 10, 21, 25}
40+
Dim target = 63
41+
Dim expected = {21, 21, 21}
42+
Assert.Equal(expected, FindFewestCoins(coins, target))
43+
End Sub
44+
45+
<Fact(Skip:="Remove this Skip property to run this test")>
46+
Public Sub Large_target_values()
47+
Dim coins = {1, 2, 5, 10, 20, 50, 100}
48+
Dim target = 999
49+
Dim expected = {2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100}
50+
Assert.Equal(expected, FindFewestCoins(coins, target))
51+
End Sub
52+
53+
<Fact(Skip:="Remove this Skip property to run this test")>
54+
Public Sub Possible_change_without_unit_coins_available()
55+
Dim coins = {2, 5, 10, 20, 50}
56+
Dim target = 21
57+
Dim expected = {2, 2, 2, 5, 10}
58+
Assert.Equal(expected, FindFewestCoins(coins, target))
59+
End Sub
60+
61+
<Fact(Skip:="Remove this Skip property to run this test")>
62+
Public Sub Another_possible_change_without_unit_coins_available()
63+
Dim coins = {4, 5}
64+
Dim target = 27
65+
Dim expected = {4, 4, 4, 5, 5, 5}
66+
Assert.Equal(expected, FindFewestCoins(coins, target))
67+
End Sub
68+
69+
<Fact(Skip:="Remove this Skip property to run this test")>
70+
Public Sub No_coins_make_0_change()
71+
Dim coins = {1, 5, 10, 21, 25}
72+
Dim target = 0
73+
Assert.Empty(FindFewestCoins(coins, target))
74+
End Sub
75+
76+
<Fact(Skip:="Remove this Skip property to run this test")>
77+
Public Sub Error_testing_for_change_smaller_than_the_smallest_of_coins()
78+
Dim coins = {5, 10}
79+
Dim target = 3
80+
Assert.Throws(Of ArgumentException)(Function() FindFewestCoins(coins, target))
81+
End Sub
82+
83+
<Fact(Skip:="Remove this Skip property to run this test")>
84+
Public Sub Error_if_no_combination_can_add_up_to_target()
85+
Dim coins = {5, 10}
86+
Dim target = 94
87+
Assert.Throws(Of ArgumentException)(Function() FindFewestCoins(coins, target))
88+
End Sub
89+
90+
<Fact(Skip:="Remove this Skip property to run this test")>
91+
Public Sub Cannot_find_negative_change_values()
92+
Dim coins = {1, 2, 5}
93+
Dim target = -5
94+
Assert.Throws(Of ArgumentException)(Function() FindFewestCoins(coins, target))
95+
End Sub
96+
End Class

0 commit comments

Comments
 (0)