Avatar of TheSmartnik

TheSmartnik's solution

to Pangram in the D Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 D page 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.

static if (all_tests_enabled)

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 D facilities in preference to writing your own low-level algorithms or facilities by hand. DRefLanguage and DReference are references to the D language and D standard library.

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

module pangram;

unittest
{
    immutable bool all_test_enabled = false;

    assert(!isPangram(""));

    static if (all_tests_enabled) {
    assert(isPangram("the quick brown fox jumps over the lazy dog"));
    // missing x
    assert(!isPangram("a quick movement of the enemy will jeopardize five gunboats"));
    assert(!isPangram("the quick brown fish jumps over the lazy dog"));
    // test underscores
    assert(isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
    // test pangram with numbers
    assert(isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
    // test missing letters replaced by numbers
    assert(!isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
    // test pangram with mixed case and punctuation
    assert(isPangram("\"Five quacking Zephyrs jolt my wax bed\""));
    // pangram with non-ascii characters
    assert(isPangram("Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich"));
    }
}
module pangram;

import std.ascii;
import std.string;
import std.regex;
import std.stdio;
import std.typecons : Yes;
import std.conv;
import std.algorithm.iteration;
import std.algorithm.sorting;
import std.range;

bool isPangram(string text)
{
    auto count = text.toLower.
        filter!(a => a.isAlpha).
        to!string.splitter!(Yes.keepSeparators)(regex("[a-z]", "g")).
        filter!(a => !a.empty).
        array.
        sort.
        uniq.
        count;

    return count == 26;
}

unittest
{
    immutable bool all_tests_enabled = true;

    assert(!isPangram(""));

    static if (all_tests_enabled) {
    assert(isPangram("the quick brown fox jumps over the lazy dog"));
    // missing x
    assert(!isPangram("a quick movement of the enemy will jeopardize five gunboats"));
    assert(!isPangram("the quick brown fish jumps over the lazy dog"));
    // test underscores
    assert(isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
    // test pangram with numbers
    assert(isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
    // test missing letters replaced by numbers
    assert(!isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
    // test pangram with mixed case and punctuation
    assert(isPangram("\"Five quacking Zephyrs jolt my wax bed\""));
    // pangram with non-ascii characters
    assert(isPangram("Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich"));
    }
}

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?