From 4ffde64dfdf82378839a04987efdd34e4cbdbbec Mon Sep 17 00:00:00 2001 From: Is0metry Date: Sun, 10 Sep 2017 15:28:10 -0500 Subject: [PATCH] Implementing pangram. (#148) * Implementing pangram. * completed implementation of pangram. * forgot the config * enforce repo style * further enforced style conventions. * forgot a semicolon. * updated config.json (sorry about that). * changed function input to const reference. --- CMakeLists.txt | 1 + config.json | 11 +++++++ exercises/pangram/CMakeLists.txt | 52 ++++++++++++++++++++++++++++++ exercises/pangram/example.cpp | 35 ++++++++++++++++++++ exercises/pangram/example.h | 11 +++++++ exercises/pangram/pangram_test.cpp | 50 ++++++++++++++++++++++++++++ 6 files changed, 160 insertions(+) create mode 100644 exercises/pangram/CMakeLists.txt create mode 100644 exercises/pangram/example.cpp create mode 100644 exercises/pangram/example.h create mode 100644 exercises/pangram/pangram_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fb0f0315e..1e42a31ae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ foreach(exercise allergies atbash-cipher bracket-push + pangram ) set(exercise_dir exercises/${exercise}) travis_fixup(${exercise}) diff --git a/config.json b/config.json index 0c59e946f..96656b35e 100644 --- a/config.json +++ b/config.json @@ -423,6 +423,17 @@ "pattern matching", "stacks" ] + }, + { + "uuid": "ea8d6224-0440-6d80-1569-a04127908a200d2ecf0", + "slug": "pangram", + "core": false, + "unlocked_by": null, + "difficulty": 2, + "topics": [ + "control-flow (loops)", + "strings" + ] } ] } diff --git a/exercises/pangram/CMakeLists.txt b/exercises/pangram/CMakeLists.txt new file mode 100644 index 000000000..97b347ff3 --- /dev/null +++ b/exercises/pangram/CMakeLists.txt @@ -0,0 +1,52 @@ +# 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.59 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() + +# Build executable from sources and headers +add_executable(${exercise} ${file}_test.cpp ${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/exercises/pangram/example.cpp b/exercises/pangram/example.cpp new file mode 100644 index 000000000..d705f10f1 --- /dev/null +++ b/exercises/pangram/example.cpp @@ -0,0 +1,35 @@ +#include "pangram.h" +#include + +namespace +{ + +bool found_letter(char a, const std::string &s) +{ + for(auto c: s) { + if(tolower(c) ==a) { + return true; + } + } + return false; +} + +} + +namespace pangram +{ + +bool is_pangram(const std::string &s) +{ + char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; + for(char l : letters) + { + if(!found_letter(l, s)) + { + return false; + } + } + return true; +} + +} diff --git a/exercises/pangram/example.h b/exercises/pangram/example.h new file mode 100644 index 000000000..e173321e3 --- /dev/null +++ b/exercises/pangram/example.h @@ -0,0 +1,11 @@ +#if !defined(PANGRAM_H) +#define PANGRAM_H +#include + +namespace pangram +{ + +bool is_pangram(const std::string &s); + +} +#endif diff --git a/exercises/pangram/pangram_test.cpp b/exercises/pangram/pangram_test.cpp new file mode 100644 index 000000000..a1f140862 --- /dev/null +++ b/exercises/pangram/pangram_test.cpp @@ -0,0 +1,50 @@ +#include "pangram.h" +#define BOOST_TEST_MAIN +#include + +BOOST_AUTO_TEST_CASE(sentence_empty) +{ + BOOST_REQUIRE(!pangram::is_pangram("")); +} + +#ifdef EXERCISM_RUN_ALL_TESTS +BOOST_AUTO_TEST_CASE(pangram_with_only_lower_case) +{ + BOOST_REQUIRE(pangram::is_pangram("the quick brown fox jumps over the lazy dog")); +} + +BOOST_AUTO_TEST_CASE(missing_character_x) +{ + BOOST_REQUIRE(!pangram::is_pangram("a quick movement of the enemy will jeopardize five gunboats")); +} + +BOOST_AUTO_TEST_CASE(another_missing_x) +{ + BOOST_REQUIRE(!pangram::is_pangram("the quick brown fish jumps over the lazy dog")); +} + +BOOST_AUTO_TEST_CASE(pangram_with_underscores) +{ + BOOST_REQUIRE(pangram::is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")); +} + +BOOST_AUTO_TEST_CASE(pangram_with_numbers) +{ + BOOST_REQUIRE(pangram::is_pangram("the 1 quick brown fox jumps over the 2 lazy dogs")); +} + +BOOST_AUTO_TEST_CASE(missing_letters_replaced_with_numbers) +{ + BOOST_REQUIRE(!pangram::is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")); +} + +BOOST_AUTO_TEST_CASE(pangram_with_mixed_case_and_punctuation) +{ + BOOST_REQUIRE(pangram::is_pangram("\"Five quacking Zephyrs jolt my wax bed.\"")); +} + +BOOST_AUTO_TEST_CASE(upper_and_lower_should_not_be_counted_seperately) +{ + BOOST_REQUIRE(!pangram::is_pangram("the quick brown fox jumps over with lazy FX")); +} +#endif