Skip to content

Commit

Permalink
Implementing pangram. (exercism#148)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Is0metry authored and arcuru committed Sep 10, 2017
1 parent 845b8f2 commit 4ffde64
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ foreach(exercise
allergies
atbash-cipher
bracket-push
pangram
)
set(exercise_dir exercises/${exercise})
travis_fixup(${exercise})
Expand Down
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
}
52 changes: 52 additions & 0 deletions exercises/pangram/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
35 changes: 35 additions & 0 deletions exercises/pangram/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "pangram.h"
#include <cctype>

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;
}

}
11 changes: 11 additions & 0 deletions exercises/pangram/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if !defined(PANGRAM_H)
#define PANGRAM_H
#include <string>

namespace pangram
{

bool is_pangram(const std::string &s);

}
#endif
50 changes: 50 additions & 0 deletions exercises/pangram/pangram_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "pangram.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

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

0 comments on commit 4ffde64

Please sign in to comment.