Skip to content

Commit

Permalink
exercises(pangram): test more non-alphanumeric printable ASCII (exerc…
Browse files Browse the repository at this point in the history
…ism#361)

Check the handling of non-alphanumeric ASCII input more thoroughly.

A faulty solution from the wild that is caught by this test but not by
the others:

    const std = @import("std");

    pub fn isPangram(str: []const u8) bool {
        var seen = std.bit_set.StaticBitSet('z' - 'a' + 1).initEmpty();
        for (str) |c| {
            const index = std.math.sub(u8, std.ascii.toLower(c), 'a') catch continue;
            // if  c > 'z'  then index > 25
            seen.set(index); // panics when index >= 26
        }
        return ~seen.mask == 0;
    }
  • Loading branch information
MatthijsBlom authored Nov 30, 2023
1 parent 5b439e7 commit 692936e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions exercises/practice/pangram/test_pangram.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ test "mixed case and punctuation" {
test "a-m and A-M are 26 different characters but not a pangram" {
try testing.expect(!pangram.isPangram("abcdefghijklm ABCDEFGHIJKLM"));
}

test "non-alphanumeric printable ASCII" {
try testing.expect(!pangram.isPangram(" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
}

0 comments on commit 692936e

Please sign in to comment.