Avatar of fu7mu4

fu7mu4's solution

to Pangram in the C++ Track

Published at Sep 28 2019 · 0 comments
Instructions
Test suite
Solution

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ 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. 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.

#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 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.

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

pangram_test.cpp

#include "pangram.h"
#include "test/catch.hpp"

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_seperately")
{
    REQUIRE(!pangram::is_pangram("the quick brown fox jumps over with lazy FX"));
}
#endif

pangram.h

#if !defined(PANGRAM_H)
#define PANGRAM_H
#include <string>
#include <unordered_map>

#define EXERCISM_RUN_ALL_TESTS

namespace pangram {
  bool is_pangram(std::string input);
}  // namespace pangram

#endif // PANGRAM_H

pangram.cpp

#include "pangram.h"

namespace pangram {

  bool is_registered(std::unordered_map<char, bool> map, char key){
    return ( map.count(key) > 0);
  }

  bool is_full(std::unordered_map<char, bool> map){
    return ( map.size() >= 26 );
  }
  
  bool is_pangram(std::string input){

    std::unordered_map<char, bool> alphabet;
    
    if(input.length() < 26 ) return false;

    for (char check : input) {
      if ( std::isalpha(check) ) {
	char c = std::tolower(check);
	if (! is_registered(alphabet, c )) {
	  alphabet[c] = true;
	  if ( is_full( alphabet ) ) return true;
        }
      }
    }
    return false;
  }
}  // namespace pangram

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?