🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ludoviko

ludoviko's solution

to Pangram in the Java Track

Published at Feb 28 2021 · 2 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.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

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.

PangramCheckerTest.java

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PangramCheckerTest {

    private PangramChecker pangramChecker;

    @Before
    public void setup() {
        pangramChecker = new PangramChecker();
    }

    @Test
    public void emptySentenceIsNotPangram() {
        assertFalse(pangramChecker.isPangram(""));
    }

    @Ignore("Remove to run test")
    @Test
    public void perfectLowerCasePhraseIsPangram() {
        assertTrue(pangramChecker.isPangram("abcdefghijklmnopqrstuvwxyz"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseWithOnlyLowerCaseIsPangram() {
        assertTrue(pangramChecker.isPangram("the quick brown fox jumps over the lazy dog"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseMissingCharacterXIsNotPangram() {
        assertFalse(pangramChecker.isPangram("a quick movement of the enemy will jeopardize five gunboats"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseMissingCharacterHIsNotPangram() {
        assertFalse(pangramChecker.isPangram("five boxing wizards jump quickly at it"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseWithUnderscoresIsPangram() {
        assertTrue(pangramChecker.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseWithNumbersIsPangram() {
        assertTrue(pangramChecker.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseWithMissingLettersReplacedByNumbersIsNotPangram() {
        assertFalse(pangramChecker.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
    }

    @Ignore("Remove to run test")
    @Test
    public void phraseWithMixedCaseAndPunctuationIsPangram() {
        assertTrue(pangramChecker.isPangram("\"Five quacking Zephyrs jolt my wax bed.\""));
    }

    @Ignore("Remove to run test")
    @Test
    public void caseInsensitivePhraseIsNotPangram() {
        assertFalse(pangramChecker.isPangram("the quick brown fox jumps over with lazy FX"));
    }
}
import java.util.BitSet;

public class PangramChecker {
    public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

    public  boolean isPangram(String string) {
        // Edge cases.
        if (string ==  null || string.isEmpty()) {
            return false;
        }

        BitSet bitSet = new BitSet(ALPHABET.length());

        for (int i = 0; i < string.length(); i++) {
            if (Character.isLetter(string.charAt(i))) {
                if (Character.isUpperCase(string.charAt(i))) {
                    bitSet.set(string.charAt(i) - 'A');
                } else {
                    bitSet.set(string.charAt(i) - 'a');
                }
            }
        }

        // Check whether all letters are in the set.
        return bitSet.cardinality() == ALPHABET.length();
    }
}

Community comments

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

Nice, but consider checking for every letter appearing once (after you get it into all ASCII lowercase letters) with Java 8's functional features like: string.chars().distinct().count() == 26

This way you don't have to handle an array or have a string containing all the letters.

Avatar of ludoviko

The purpose od adding an alphabet is parametrization, the user may add what ever alphabet or other letters to.

If the alphabet is missing, from the code would be impossible to guess what alphabet is the pangram targetting.

ludoviko's Reflection

The tutor proposed a solutions with streams.