Skip to content
Closed
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
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,19 @@
"comparisons"
],
"difficulty": 6
},
{
"slug": "change",
"name": "Change",
"uuid": "e85b5b3e-0ac4-44c4-8b34-fb05b540dc26",
"practices": [],
"prerequisites": [
"exceptions",
"lists",
"numbers",
"loops"
],
"difficulty": 5
}
],
"foregone": [
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."
21 changes: 21 additions & 0 deletions exercises/practice/change/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"marcelweikum"
],
"files": {
"solution": [
"change.cpp",
"change.h"
],
"test": [
"change_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"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"
}
46 changes: 46 additions & 0 deletions exercises/practice/change/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <vector>

#include "change.h"

namespace change {

std::vector<int> find_fewest_coins(const std::vector<int>& coins, int target) {
if (target < 0) {
throw std::domain_error("target can't be negative");
}
if (target == 0) {
return {};
}
if (coins.empty()) {
throw std::domain_error("can't make target with given coins");
}

std::vector<int> dp(target + 1, std::numeric_limits<int>::max());
std::vector<std::vector<int>> solution(target + 1);

dp[0] = 0;

for (int i = 1; i <= target; ++i) {
for (int coin : coins) {
if (i >= coin && dp[i - coin] != std::numeric_limits<int>::max()) {
if (dp[i - coin] + 1 < dp[i]) {
dp[i] = dp[i - coin] + 1;
solution[i] = solution[i - coin];
solution[i].push_back(coin);
}
}
}
}

if (dp[target] == std::numeric_limits<int>::max()) {
throw std::domain_error("can't make target with given coins");
}

std::sort(solution[target].begin(), solution[target].end());
return solution[target];
}

} // namespace change
9 changes: 9 additions & 0 deletions exercises/practice/change/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <vector>

namespace change {

std::vector<int> find_fewest_coins(const std::vector<int>& coins, int target);

} // namespace change
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"
67 changes: 67 additions & 0 deletions exercises/practice/change/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
# Treat warnings as errors
# Treat type conversion warnings C4244 and C4267 as level 4 warnings, i.e. ignore them in level 3
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS
COMPILE_FLAGS "/WX /w44244 /w44267")
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
7 changes: 7 additions & 0 deletions exercises/practice/change/change.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "change.h"

namespace change {

// TODO: add your solution here

} // namespace change
7 changes: 7 additions & 0 deletions exercises/practice/change/change.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace change {

// TODO: add your solution here

} // namespace change
83 changes: 83 additions & 0 deletions exercises/practice/change/change_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "change.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

TEST_CASE("change_for_1_cent", "[d0ebd0e1-9d27-4609-a654-df5c0ba1d83a]") {
REQUIRE(change::find_fewest_coins({1, 5, 10, 25}, 1) ==
std::vector<int>{1});
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("single_coin_change", "[36887bea-7f92-4a9c-b0cc-c0e886b3ecc8]") {
REQUIRE(change::find_fewest_coins({1, 5, 10, 25, 100}, 25) ==
std::vector<int>{25});
}

TEST_CASE("multiple_coin_change", "[cef21ccc-0811-4e6e-af44-f011e7eab6c6]") {
REQUIRE(change::find_fewest_coins({1, 5, 10, 25, 100}, 15) ==
std::vector<int>{5, 10});
}

TEST_CASE("change_with_Lilliputian_Coins",
"[d60952bc-0c1a-4571-bf0c-41be72690cb3]") {
REQUIRE(change::find_fewest_coins({1, 4, 15, 20, 50}, 23) ==
std::vector<int>{4, 4, 15});
}

TEST_CASE("change_with_Lower_Elbonia_Coins",
"[408390b9-fafa-4bb9-b608-ffe6036edb6c]") {
REQUIRE(change::find_fewest_coins({1, 5, 10, 21, 25}, 63) ==
std::vector<int>{21, 21, 21});
}

TEST_CASE("large_target_values", "[7421a4cb-1c48-4bf9-99c7-7f049689132f]") {
REQUIRE(change::find_fewest_coins({1, 2, 5, 10, 20, 50, 100}, 999) ==
std::vector<int>{2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100,
100, 100, 100});
}

TEST_CASE("possible_change_without_unit_coins",
"[f79d2e9b-0ae3-4d6a-bb58-dc978b0dba28]") {
REQUIRE(change::find_fewest_coins({2, 5, 10, 20, 50}, 21) ==
std::vector<int>{2, 2, 2, 5, 10});
}

TEST_CASE("another_possible_change_without_unit_coins",
"[9a166411-d35d-4f7f-a007-6724ac266178]") {
REQUIRE(change::find_fewest_coins({4, 5}, 27) ==
std::vector<int>{4, 4, 4, 5, 5, 5});
}

TEST_CASE("a_greedy_approach_is_not_optimal",
"[ce0f80d5-51c3-469d-818c-3e69dbd25f75]") {
REQUIRE(change::find_fewest_coins({1, 10, 11}, 20) ==
std::vector<int>{10, 10});
}

TEST_CASE("no_coins_make_0_change", "[bbbcc154-e9e9-4209-a4db-dd6d81ec26bb]") {
REQUIRE(change::find_fewest_coins({1, 5, 10, 21, 25}, 0) ==
std::vector<int>{});
}

TEST_CASE("change_smaller_than_smallest_coin",
"[c8b81d5a-49bd-4b61-af73-8ee5383a2ce1]") {
REQUIRE_THROWS_AS(change::find_fewest_coins({5, 10}, 3), std::domain_error);
}

TEST_CASE("no_combination_can_make_target",
"[3c43e3e4-63f9-46ac-9476-a67516e98f68]") {
REQUIRE_THROWS_AS(change::find_fewest_coins({5, 10}, 94),
std::domain_error);
}

TEST_CASE("cannot_find_negative_change",
"[8fe1f076-9b2d-4f44-89fe-8a6ccd63c8f3]") {
REQUIRE_THROWS_AS(change::find_fewest_coins({1, 2, 5}, -5),
std::domain_error);
}

#endif
Loading