Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Pangram in the Perl 6 Track

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.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

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

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Pangram;
plan 10;

my Version:D $version = v3;

if Pangram.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nPangram is {Pangram.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;
for $c-data<cases>».<cases>».Array.flat {
  given is-pangram .<input><sentence> -> $result {
    subtest .<description>, {
      plan 2;
      isa-ok $result, Bool;
      is-deeply $result, .<expected>, 'Result matches expected';
    }
  }
}

=head2 Canonical Data
=begin code
{
  "exercise": "pangram",
  "comments": [
    "A pangram is a sentence using every letter of the alphabet at least once."
  ],
  "version": "1.4.0",
  "cases": [
    {
      "description": "Check if the given string is an pangram",
      "comments": [
        "Output should be a boolean denoting if the string is a pangram or not."
      ],
      "cases": [
        {
          "description": "sentence empty",
          "property": "isPangram",
          "input": {
            "sentence": ""
          },
          "expected": false
        },
        {
          "description": "recognizes a perfect lower case pangram",
          "property": "isPangram",
          "input": {
            "sentence": "abcdefghijklmnopqrstuvwxyz"
          },
          "expected": true
        },
        {
          "description": "pangram with only lower case",
          "property": "isPangram",
          "input": {
            "sentence": "the quick brown fox jumps over the lazy dog"
          },
          "expected": true
        },
        {
          "description": "missing character 'x'",
          "property": "isPangram",
          "input": {
            "sentence": "a quick movement of the enemy will jeopardize five gunboats"
          },
          "expected": false
        },
        {
          "description": "another missing character, e.g. 'h'",
          "property": "isPangram",
          "input": {
            "sentence": "five boxing wizards jump quickly at it"
          },
          "expected": false
        },
        {
          "description": "pangram with underscores",
          "property": "isPangram",
          "input": {
            "sentence": "the_quick_brown_fox_jumps_over_the_lazy_dog"
          },
          "expected": true
        },
        {
          "description": "pangram with numbers",
          "property": "isPangram",
          "input": {
            "sentence": "the 1 quick brown fox jumps over the 2 lazy dogs"
          },
          "expected": true
        },
        {
          "description": "missing letters replaced by numbers",
          "property": "isPangram",
          "input": {
            "sentence": "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"
          },
          "expected": false
        },
        {
          "description": "pangram with mixed case and punctuation",
          "property": "isPangram",
          "input": {
            "sentence": "\"Five quacking Zephyrs jolt my wax bed.\""
          },
          "expected": true
        },
        {
          "description": "upper and lower case versions of the same character should not be counted separately",
          "property": "isPangram",
          "input": {
            "sentence": "the quick brown fox jumps over with lazy FX"
          },
          "expected": false
        }
      ]
    }
  ]
}
=end code
unit module Pangram:ver<1>;

sub is-pangram (Str:D $string --> Bool) is export {
    $string.lc.subst(/<-[ a .. z ]>/, '', :g).comb.Set.elems eq 26
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?