Skip to content

Pangram

Decide whether a sentence uses every letter of the English alphabet at least once, ignoring case.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: pangram.nomi
//# Pangrams: sentences that use every letter of the alphabet.

/// Whether `sentence` uses every letter of the English alphabet at least
/// once, ignoring case.
pub fn is_pangram(sentence: String) -> Bool
    letters =
        sentence
        |> String.fold_case()
        |> String.to_codepoints()
        |> Iter.to_set()

    Iter.all_match('a'..='z', &(&1 in letters))
end

// FILE: pangram_test.nomi
import pangram.is_pangram

test "pangrams"
    [
        ("empty sentence",     "",                                            False),
        ("perfect lower case", "abcdefghijklmnopqrstuvwxyz",                  True),
        ("only lower case",    "the quick brown fox jumps over the lazy dog", True),
        (
            "missing the letter 'x'",
            "a quick movement of the enemy will jeopardize five gunboats",
            False,
        ),
        ("missing the letter 'h'", "five boxing wizards jump quickly at it",           False),
        ("with underscores",       "the_quick_brown_fox_jumps_over_the_lazy_dog",      True),
        ("with numbers",           "the 1 quick brown fox jumps over the 2 lazy dogs", True),
        (
            "missing letters replaced by numbers",
            "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog",
            False,
        ),
        ("mixed case and punctuation", "\"Five quacking Zephyrs jolt my wax bed.\"", True),
        (
            "a-m and A-M are 26 different characters but not a pangram",
            "abcdefghijklm ABCDEFGHIJKLM",
            False,
        ),
    ]
    |> subtests (description, sentence, expected), description
        assert is_pangram(sentence) == expected
    end
end
  • The sentence becomes a Set<Codepoint> and the check is Iter.all_match('a'..='z', ...) over a codepoint range. Codepoint literals and ranges are what style §20 recommends for ASCII enumeration. Set.is_subset_of(Iter.to_set('a'..='z'), letters) works as well; I picked Iter.all_match because it reads as the problem statement. The predicate is &(&1 in letters), a capture.
  • Case is ignored with String.fold_case, the function for comparing without case, rather than String.lowercase.
  • The case “case insensitive” (uuid 2577bf54) is left out because canonical data replaces it with “a-m and A-M are 26 different characters but not a pangram” (uuid 7138e389), which is included.