Skip to content
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

C++ pangram #178

Merged
merged 4 commits into from
Jul 26, 2023
Merged
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
20 changes: 20 additions & 0 deletions cpp/.cmake-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# How wide to allow formatted cmake files
line_width: 120

# How many spaces to tab for indent
tab_size: 2

# If arglists are longer than this, break them always.
max_subargs_per_line: 3

# Additional FLAGS and KWARGS for custom commands
additional_commands:
foo:
flags: [BAR, BAZ]
kwargs:
HEADERS: '*'
SOURCES: '*'
DEPENDS: '*'

# vim:ft=yaml
1 change: 1 addition & 0 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@
- [perfect-numbers](./perfect-numbers/README.md)
- [scrabble-score](./scrabble-score/README.md)
- [gigasecond](./gigasecond/README.md)
- [pangram](./pangram/README.md)
28 changes: 28 additions & 0 deletions cpp/pangram/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"authors": [
"Is0metry"
],
"contributors": [
"elyashiv",
"jackhughesweb",
"KevinWMatthews",
"LegalizeAdulthood",
"patricksjackson"
],
"files": {
"solution": [
"pangram.cpp",
"pangram.h"
],
"test": [
"pangram_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Determine if a sentence is a pangram.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Pangram"
}
1 change: 1 addition & 0 deletions cpp/pangram/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"cpp","exercise":"pangram","id":"d50b6a7d0584476faa21f1d3118aec11","url":"https://exercism.org/tracks/cpp/exercises/pangram","handle":"vpayno","is_requester":true,"auto_approve":false}
63 changes: 63 additions & 0 deletions cpp/pangram/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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}.hpp $<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}.hpp)
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}.hpp 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
if(${MSVC})
set_target_properties(${exercise} PROPERTIES COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(
test_${exercise} ALL
DEPENDS ${exercise}
COMMAND ${exercise} --success)
52 changes: 52 additions & 0 deletions cpp/pangram/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Help

## Running the tests

Running the tests involves running `cmake -G` and then using the build command appropriate for your platform.
Detailed instructions on how to do this can be found on the [Running the Tests][cpp-tests-instructions] page for C++ on exercism.org.

## Passing the Tests

Get the first test compiling, linking and passing by following the [three rules of test-driven development][three-laws-of-tdd].
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++17 facilities in preference to writing your own low-level algorithms or facilities by hand.

[cpp-tests-instructions]: https://exercism.org/docs/tracks/cpp/tests
[three-laws-of-tdd]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd

## Submitting your solution

You can submit your solution using the `exercism submit pangram.cpp pangram.h` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [C++ track's documentation](https://exercism.org/docs/tracks/cpp)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [`c++-faq` tag on StackOverflow](https://stackoverflow.com/tags/c%2b%2b-faq/info)
- [C++ FAQ from isocpp.com](https://isocpp.org/faq)
- [CppReference](http://en.cppreference.com/) is a wiki reference to the C++ language and standard library
- [C traps and pitfalls](http://www.slideshare.net/LegalizeAdulthood/c-traps-and-pitfalls-for-c-programmers) is useful if you are new to C++, but have programmed in C
23 changes: 23 additions & 0 deletions cpp/pangram/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.PHONY: clean
clean:
rm -rf ./build

.PHONY: test
test: clean
mkdir -pv ./build

@printf "\n"
@# each line is executed in a subshell, we have to daisy chain them so they
@# run in the build directory
cd ./build; export LDFLAGS="-lgcov --coverage" CXXFLAGS="--coverage"; cmake -DEXERCISM_RUN_ALL_TESTS=1 -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -G 'Unix Makefiles' ../; if make; then printf "\n=== All Tests Passed ===\n"; else printf "\n=== Test Failure ===\n"; false; fi

.PHONY: coverage
coverage: test
@printf "\n"
find . -regextype posix-egrep -regex "^.*(tests-main|CompilerId).*[.](gcda|gcno)$$" -print -delete

@printf "\n"
find . -regextype posix-egrep -regex "^.*[.](gcda|gcno)15752"

@printf "\n"
gcovr --print-summary
53 changes: 53 additions & 0 deletions cpp/pangram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Pangram

Welcome to Pangram on Exercism's C++ Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Introduction

You work for a company that sells fonts through their website.
They'd like to show a different sentence each time someone views a font on their website.
To give a comprehensive sense of the font, the random sentences should use **all** the letters in the English alphabet.

They're running a competition to get suggestions for sentences that they can use.
You're in charge of checking the submissions to see if they are valid.

```exercism/note
Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter".

The best known English pangram is:

> The quick brown fox jumps over the lazy dog.
```

## Instructions

Your task is to figure out if a sentence is a pangram.

A pangram is a sentence using every letter of the alphabet at least once.
It is case insensitive, so it doesn't matter if a letter is lower-case (e.g. `k`) or upper-case (e.g. `K`).

For this exercise we only use the basic letters used in the English alphabet: `a` to `z`.

## Source

### Created by

- @Is0metry

### Contributed to by

- @elyashiv
- @jackhughesweb
- @KevinWMatthews
- @LegalizeAdulthood
- @patricksjackson

### Based on

Wikipedia - https://en.wikipedia.org/wiki/Pangram

### My Solution

- [my solution]()
- [run-tests](./run-tests-cpp.txt)
17 changes: 17 additions & 0 deletions cpp/pangram/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/pangram.dir/pangram_test.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/pangram_test.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/pangram_test.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/pangram.dir/pangram.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/pangram.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/pangram.cpp"
},
{
"directory": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/build",
"command": "/usr/bin/c++ -DEXERCISM_RUN_ALL_TESTS -g -Wall -Wextra -Wpedantic -Werror -std=c++17 -o CMakeFiles/pangram.dir/test/tests-main.cpp.o -c /home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/test/tests-main.cpp",
"file": "/home/vpayno/git_vpayno/exercism-workspace/cpp/pangram/test/tests-main.cpp"
}
]
8 changes: 8 additions & 0 deletions cpp/pangram/compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-std=c++17
-stdlib=libstdc++
-g
-Wall
-Wextra
-Wpedantic
-Werror
-Wno-unused-parameter
34 changes: 34 additions & 0 deletions cpp/pangram/pangram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "pangram.hpp"

#include <iostream>

namespace pangram {

bool is_pangram(std::string input) {
const size_t letter_count = 26;

if (letter_count > input.size()) {
return false;
}

std::string text{};

std::transform(input.begin(), input.end(), std::back_inserter(text),
tolower);

std::map<char, bool> tracker{};

for (auto rune : text) {
if (!(bool)isalpha(rune)) {
continue;
}

tracker[rune] = true;
}

const size_t count{tracker.size()};

return letter_count == count;
}

} // namespace pangram
14 changes: 14 additions & 0 deletions cpp/pangram/pangram.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if !defined(PANGRAM_HPP)
#define PANGRAM_HPP

#include <algorithm>
#include <map>
#include <string>

namespace pangram {

bool is_pangram(std::string input);

} // namespace pangram

#endif // PANGRAM_HPP
14 changes: 14 additions & 0 deletions cpp/pangram/pangram.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<key>diagnostics</key>
<array>
</array>
<key>files</key>
<array>
</array>
</dict>
</plist>
47 changes: 47 additions & 0 deletions cpp/pangram/pangram_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "pangram.hpp"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

TEST_CASE("sentence_empty") { REQUIRE(!pangram::is_pangram("")); }

#ifdef EXERCISM_RUN_ALL_TESTS
TEST_CASE("pangram_with_only_lower_case") {
REQUIRE(pangram::is_pangram("the quick brown fox jumps over the lazy dog"));
}

TEST_CASE("missing_character_x") {
REQUIRE(!pangram::is_pangram(
"a quick movement of the enemy will jeopardize five gunboats"));
}

TEST_CASE("another_missing_x") {
REQUIRE(
!pangram::is_pangram("the quick brown fish jumps over the lazy dog"));
}

TEST_CASE("pangram_with_underscores") {
REQUIRE(pangram::is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
}

TEST_CASE("pangram_with_numbers") {
REQUIRE(pangram::is_pangram(
"the 1 quick brown fox jumps over the 2 lazy dogs"));
}

TEST_CASE("missing_letters_replaced_with_numbers") {
REQUIRE(
!pangram::is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
}

TEST_CASE("pangram_with_mixed_case_and_punctuation") {
REQUIRE(pangram::is_pangram("\"Five quacking Zephyrs jolt my wax bed.\""));
}

TEST_CASE("upper_and_lower_should_not_be_counted_separately") {
REQUIRE(
!pangram::is_pangram("the quick brown fox jumps over with lazy FX"));
}
#endif
Loading