-
-
Notifications
You must be signed in to change notification settings - Fork 228
Added "book-store" exercise #974
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
1768c95
91dc5bd
38968e7
2babf30
9c88cb3
9ed3952
866eab1
33471d4
d5f0841
0ccfd7e
f934df1
9ff5dcc
ac75930
01c1dd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Instructions | ||
|
||
To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts on multiple book purchases. | ||
|
||
One copy of any of the five books costs $8. | ||
|
||
If, however, you buy two different books, you get a 5% discount on those two books. | ||
|
||
If you buy 3 different books, you get a 10% discount. | ||
|
||
If you buy 4 different books, you get a 20% discount. | ||
|
||
If you buy all 5, you get a 25% discount. | ||
|
||
Note that if you buy four books, of which 3 are different titles, you get a 10% discount on the 3 that form part of a set, but the fourth book still costs $8. | ||
|
||
Your mission is to write code to calculate the price of any conceivable shopping basket (containing only books of the same series), giving as big a discount as possible. | ||
|
||
For example, how much does this basket of books cost? | ||
|
||
- 2 copies of the first book | ||
- 2 copies of the second book | ||
- 2 copies of the third book | ||
- 1 copy of the fourth book | ||
- 1 copy of the fifth book | ||
|
||
One way of grouping these 8 books is: | ||
|
||
- 1 group of 5 (1st, 2nd,3rd, 4th, 5th) | ||
- 1 group of 3 (1st, 2nd, 3rd) | ||
|
||
This would give a total of: | ||
|
||
- 5 books at a 25% discount | ||
- 3 books at a 10% discount | ||
|
||
Resulting in: | ||
|
||
- 5 × (100% - 25%) × $8 = 5 × $6.00 = $30.00, plus | ||
- 3 × (100% - 10%) × $8 = 3 × $7.20 = $21.60 | ||
|
||
Which equals $51.60. | ||
|
||
However, a different way to group these 8 books is: | ||
|
||
- 1 group of 4 books (1st, 2nd, 3rd, 4th) | ||
- 1 group of 4 books (1st, 2nd, 3rd, 5th) | ||
|
||
This would give a total of: | ||
|
||
- 4 books at a 20% discount | ||
- 4 books at a 20% discount | ||
|
||
Resulting in: | ||
|
||
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60, plus | ||
- 4 × (100% - 20%) × $8 = 4 × $6.40 = $25.60 | ||
|
||
Which equals $51.20. | ||
|
||
And $51.20 is the price with the biggest discount. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"authors": [ | ||
"marcelweikum" | ||
], | ||
"files": { | ||
"solution": [ | ||
"book_store.cpp", | ||
"book_store.h" | ||
], | ||
"test": [ | ||
"book_store_test.cpp" | ||
], | ||
"example": [ | ||
".meta/example.cpp", | ||
".meta/example.h" | ||
] | ||
}, | ||
"blurb": "To try and encourage more sales of different books from a popular 5 book series, a bookshop has decided to offer discounts of multiple-book purchases.", | ||
"source": "Inspired by the harry potter kata from Cyber-Dojo.", | ||
"source_url": "https://cyber-dojo.org" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <array> | ||
#include <limits> | ||
#include <map> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "book_store.h" | ||
|
||
namespace book_store { | ||
|
||
static const int PRICE_AFTER_DISCOUNT[6] = {0, 800, 1520, 2160, 2560, 3000}; | ||
|
||
static int dfs(const std::array<int, 5>& state, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to restrict visibility of this function? Then put it into an anonymous namespace please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the static for simplicity or do you prefer internal functions and variables? |
||
std::map<std::array<int, 5>, int>& memo) { | ||
auto it = memo.find(state); | ||
if (it != memo.end()) return it->second; | ||
|
||
bool empty = true; | ||
for (int c : state) { | ||
if (c > 0) { | ||
empty = false; | ||
break; | ||
} | ||
} | ||
marcelweikum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (empty) return memo[state] = 0; | ||
marcelweikum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
int best = std::numeric_limits<int>::max(); | ||
|
||
std::vector<int> idx; | ||
for (int i = 0; i < 5; ++i) { | ||
if (state[i] > 0) idx.push_back(i); | ||
} | ||
int n = static_cast<int>(idx.size()); | ||
|
||
int maxMask = 1 << n; | ||
for (int mask = 1; mask < maxMask; ++mask) { | ||
std::array<int, 5> next = state; | ||
int k = 0; | ||
for (int i = 0; i < n; ++i) { | ||
if (mask & (1 << i)) { | ||
next[idx[i]]--; | ||
++k; | ||
} | ||
} | ||
int cost = PRICE_AFTER_DISCOUNT[k] + dfs(next, memo); | ||
if (cost < best) best = cost; | ||
} | ||
|
||
return memo[state] = best; | ||
marcelweikum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
int total(const std::vector<int>& basket) { | ||
std::array<int, 5> counts = {0, 0, 0, 0, 0}; | ||
marcelweikum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (int id : basket) { | ||
if (id < 1 || id > 5) { | ||
throw std::invalid_argument("book ID is out of range (1–5)"); | ||
} | ||
counts[id - 1]++; | ||
} | ||
|
||
std::map<std::array<int, 5>, int> memo; | ||
return dfs(counts, memo); | ||
} | ||
|
||
} // namespace book_store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
namespace book_store { | ||
|
||
int total(const std::vector<int>& basket); | ||
|
||
} // namespace book_store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 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. | ||
|
||
[17146bd5-2e80-4557-ab4c-05632b6b0d01] | ||
description = "Only a single book" | ||
|
||
[cc2de9ac-ff2a-4efd-b7c7-bfe0f43271ce] | ||
description = "Two of the same book" | ||
|
||
[5a86eac0-45d2-46aa-bbf0-266b94393a1a] | ||
description = "Empty basket" | ||
|
||
[158bd19a-3db4-4468-ae85-e0638a688990] | ||
description = "Two different books" | ||
|
||
[f3833f6b-9332-4a1f-ad98-6c3f8e30e163] | ||
description = "Three different books" | ||
|
||
[1951a1db-2fb6-4cd1-a69a-f691b6dd30a2] | ||
description = "Four different books" | ||
|
||
[d70f6682-3019-4c3f-aede-83c6a8c647a3] | ||
description = "Five different books" | ||
|
||
[78cacb57-911a-45f1-be52-2a5bd428c634] | ||
description = "Two groups of four is cheaper than group of five plus group of three" | ||
|
||
[f808b5a4-e01f-4c0d-881f-f7b90d9739da] | ||
description = "Two groups of four is cheaper than groups of five and three" | ||
|
||
[fe96401c-5268-4be2-9d9e-19b76478007c] | ||
description = "Group of four plus group of two is cheaper than two groups of three" | ||
|
||
[68ea9b78-10ad-420e-a766-836a501d3633] | ||
description = "Two each of first four books and one copy each of rest" | ||
|
||
[c0a779d5-a40c-47ae-9828-a340e936b866] | ||
description = "Two copies of each book" | ||
|
||
[18fd86fe-08f1-4b68-969b-392b8af20513] | ||
description = "Three copies of first book and two each of remaining" | ||
|
||
[0b19a24d-e4cf-4ec8-9db2-8899a41af0da] | ||
description = "Three each of first two books and two each of remaining books" | ||
|
||
[bb376344-4fb2-49ab-ab85-e38d8354a58d] | ||
description = "Four groups of four are cheaper than two groups each of five and three" | ||
|
||
[5260ddde-2703-4915-b45a-e54dbbac4303] | ||
description = "Check that groups of four are created properly even when there are more groups of three than groups of five" | ||
|
||
[b0478278-c551-4747-b0fc-7e0be3158b1f] | ||
description = "One group of one and four is cheaper than one group of two and three" | ||
|
||
[cf868453-6484-4ae1-9dfc-f8ee85bbde01] | ||
description = "One group of one and two plus three groups of four is cheaper than one group of each size" |
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}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "book_store.h" | ||
|
||
namespace book_store { | ||
|
||
// TODO: add your solution here | ||
|
||
} // namespace book_store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef BOOK_STORE_H | ||
marcelweikum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#define BOOK_STORE_H | ||
|
||
namespace book_store { | ||
|
||
// TODO: add your solution here | ||
|
||
} // namespace book_store | ||
|
||
#endif // BOOK_STORE_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
constexpr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably would make sense to use
std::array
here. I can imagine all template parameters could be automatically derived, i.e.:It looks like we're not quite consistent with uppercase vs lowercase for constants, but I'd be very much in favor of using
ALL_CAPS
only for preprocessor macros. @vaeng Do we have anything official on this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both done. Are there any ressources for coding styles for this repo? I don't want to create extra work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, screaming snake makros. I would adhere to the C++ Core Guidelines. (Which I might not have in some places where it slipped my mind) https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rl-all-caps