diff --git a/cpp/series/CMakeLists.txt b/cpp/series/CMakeLists.txt new file mode 100644 index 0000000..56a3675 --- /dev/null +++ b/cpp/series/CMakeLists.txt @@ -0,0 +1,59 @@ +# Get the exercise name from the current directory +get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME) + +# Basic CMake project +cmake_minimum_required(VERSION 2.8.11) + +# Name the project after the exercise +project(${exercise} CXX) + +# Locate Boost libraries: unit_test_framework, date_time and regex +set(Boost_USE_STATIC_LIBS ON) +set(Boost_USE_MULTITHREADED ON) +set(Boost_USE_STATIC_RUNTIME OFF) +find_package(Boost 1.55 REQUIRED COMPONENTS unit_test_framework date_time regex) + +# Enable C++11 features on gcc/clang +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)") + set(CMAKE_CXX_FLAGS "-std=c++11") +endif() + +# Configure to run all the tests? +if(${EXERCISM_RUN_ALL_TESTS}) + add_definitions(-DEXERCISM_RUN_ALL_TESTS) +endif() + +# 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() + +# Include a test helper header if it exists +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/require_equal_containers.h) + set(test_helper require_equal_containers.h) +else() + set(test_helper "") +endif() + +# Build executable from sources and headers +add_executable(${exercise} ${file}_test.cpp ${test_helper} ${exercise_cpp} ${file}.h) + +# We need boost includes +target_include_directories(${exercise} PRIVATE ${Boost_INCLUDE_DIRS}) + +# We need boost libraries +target_link_libraries(${exercise} ${Boost_LIBRARIES}) + +# Tell MSVC not to warn us about unchecked iterators in debug builds +if(${MSVC}) + set_target_properties(${exercise} PROPERTIES + COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS) +endif() + +# Run the tests on every build +add_custom_command(TARGET ${exercise} POST_BUILD COMMAND ${exercise}) diff --git a/cpp/series/README.md b/cpp/series/README.md new file mode 100644 index 0000000..f1f8679 --- /dev/null +++ b/cpp/series/README.md @@ -0,0 +1,55 @@ +# Series + +Write a program that will take a string of digits and give you all the contiguous substrings of length `n` in that string. + +For example, the string "49142" has the following 3-digit series: + +- 491 +- 914 +- 142 + +And the following 4-digit series: + +- 4914 +- 9142 + +And if you ask for a 6-digit series from a 5-digit string, you deserve +whatever you get. + +Note that these series are only required to occupy *adjacent positions* +in the input; the digits need not be *numerically consecutive*. + +## Getting Started + +Make sure you have read [the C++ page](http://exercism.io/languages/cpp) on +exercism.io. This covers the basic information on setting up the development +environment expected by the exercises. + +## Passing the Tests + +Get the first test compiling, linking and passing by following the [three +rules of test-driven development](http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd). +Create just enough structure by declaring namespaces, functions, classes, +etc., to satisfy any compiler errors and get the test to fail. Then write +just enough code to get the test to pass. Once you've done that, +uncomment the next test by moving the following line past the next test. + +```C++ +#if defined(EXERCISM_RUN_ALL_TESTS) +``` + +This may result in compile errors as new constructs may be invoked that +you haven't yet declared or defined. Again, fix the compile errors minimally +to get a failing test, then change the code minimally to pass the test, +refactor your implementation for readability and expressiveness and then +go on to the next test. + +Try to use standard C++11 facilities in preference to writing your own +low-level algorithms or facilities by hand. [CppReference](http://en.cppreference.com/) +is a wiki reference to the C++ language and standard library. If you +are new to C++, but have programmed in C, beware of +[C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers). + +## Source + +A subset of the Problem 8 at Project Euler [view source](http://projecteuler.net/problem=8) diff --git a/cpp/series/require_equal_containers.h b/cpp/series/require_equal_containers.h new file mode 100644 index 0000000..45d1213 --- /dev/null +++ b/cpp/series/require_equal_containers.h @@ -0,0 +1,88 @@ +#if !defined(REQUIRE_EQUAL_CONTAINERS_H) +#define REQUIRE_EQUAL_CONTAINERS_H + +#include +#include +#include + +#if BOOST_VERSION >= 105900 + +namespace boost +{ +namespace test_tools +{ +namespace tt_detail +{ + +// teach Boost.Test how to print std::vector +template +inline std::ostream &operator<<(std::ostream &str, std::vector const &items) +{ + str << '['; + bool first = true; + for (auto const& element : items) { + str << (!first ? "," : "") << element; + first = false; + } + return str << ']'; +} + +// teach Boost.Test how to print std::pair +template +inline std::ostream &operator<<(std::ostream &str, std::pair const& item) +{ + return str << '<' << item.first << ',' << item.second << '>'; +} + +} // namespace tt_detail +} // namespace test_tools +} // namespace boost + +#else // BOOST_VERSION < 105900 + +namespace boost +{ + +// teach Boost.Test how to print std::vector to wrap_stringstream +template +inline wrap_stringstream& +operator<<(wrap_stringstream& wrapped, std::vector const& item) +{ + wrapped << '['; + bool first = true; + for (auto const& element : item) { + wrapped << (!first ? "," : "") << element; + first = false; + } + return wrapped << ']'; +} + +// teach Boost.Test how to print std::pair to wrap_stringstream +template +inline wrap_stringstream &operator<<(wrap_stringstream &str, std::pair const& item) +{ + return str << '<' << item.first << ',' << item.second << '>'; +} + +namespace test_tools +{ + +// teach Boost.Test how to print std::pair with BOOST_REQUIRE_EQUAL +template<> +struct print_log_value> +{ + void operator()(std::ostream& ostr, std::pair const& item) + { + ostr << '<' << item.first << ',' << item.second << '>'; + } +}; + +} // namespace test_tools +} // namespace boost + +#endif // BOOST_VERSION + +#define REQUIRE_EQUAL_CONTAINERS(left_, right_) \ + BOOST_REQUIRE_EQUAL_COLLECTIONS(left_.begin(), left_.end(), right_.begin(), right_.end()) + +#endif diff --git a/cpp/series/series_test.cpp b/cpp/series/series_test.cpp new file mode 100644 index 0000000..d0b17f5 --- /dev/null +++ b/cpp/series/series_test.cpp @@ -0,0 +1,104 @@ +#include "series.h" +#define BOOST_TEST_MAIN +#include +#include +#include "require_equal_containers.h" + +using namespace std; + +BOOST_AUTO_TEST_CASE(short_digits) +{ + const vector expected{0, 1, 2, 3, 4, 5}; + + const vector actual{series::digits("012345")}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +#if defined(EXERCISM_RUN_ALL_TESTS) +BOOST_AUTO_TEST_CASE(long_digits) +{ + const vector expected{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + + const vector actual{series::digits("0123456789")}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(keeps_the_digit_order_if_reversed) +{ + const vector expected{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; + + const vector actual{series::digits("9876543210")}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(keeps_arbitrary_digit_order) +{ + const vector expected{9, 3, 6, 9, 2, 3, 4, 6, 8}; + + const vector actual{series::digits("936923468")}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_1) +{ + const vector> expected{{0}, {1}, {2}, {3}, {4}}; + + const vector> actual{series::slice("01234", 1)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_2) +{ + const vector> expected{{9, 8}, {8, 2}, {2, 7}, {7, 3}, {3, 4}, {4, 6}, {6, 3}}; + + const vector> actual{series::slice("98273463", 2)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_3) +{ + const vector> expected{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}; + + const vector> actual{series::slice("01234", 3)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_3_with_duplicate_digits) +{ + const vector> expected{{3, 1, 0}, {1, 0, 0}, {0, 0, 1}}; + + const vector> actual{series::slice("31001", 3)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_4) +{ + const vector> expected{{3, 1, 0}, {1, 0, 0}, {0, 0, 1}}; + + const vector> actual{series::slice("31001", 3)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(can_slice_by_5) +{ + const vector> expected{{8, 1, 2, 2, 8}}; + + const vector> actual{series::slice("81228", 5)}; + + BOOST_REQUIRE_EQUAL_COLLECTIONS(expected.begin(), expected.end(), actual.begin(), actual.end()); +} + +BOOST_AUTO_TEST_CASE(domain_error_if_not_enough_digits_to_slice) +{ + BOOST_REQUIRE_THROW(series::slice("01032987583", 12), domain_error); +} +#endif