Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Pangram in the PHP 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.

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar pangram/pangram_test.php
    

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_test.php

<?php

require "pangram.php";

class PangramTest extends PHPUnit\Framework\TestCase
{
    public function testSentenceEmpty()
    {
        $this->assertFalse(isPangram(''));
    }

    public function testPangramWithOnlyLowerCase()
    {
        $this->markTestSkipped();
        $this->assertTrue(isPangram('the quick brown fox jumps over the lazy dog'));
    }

    public function testMissingCharacterX()
    {
        $this->markTestSkipped();
        $this->assertFalse(isPangram('a quick movement of the enemy will jeopardize five gunboats'));
    }

    public function testAnotherMissingCharacterX()
    {
        $this->markTestSkipped();
        $this->assertFalse(isPangram('the quick brown fish jumps over the lazy dog'));
    }

    public function testPangramWithUnderscores()
    {
        $this->markTestSkipped();
        $this->assertTrue(isPangram('the_quick_brown_fox_jumps_over_the_lazy_dog'));
    }

    public function testPangramWithNumbers()
    {
        $this->markTestSkipped();
        $this->assertTrue(isPangram('the 1 quick brown fox jumps over the 2 lazy dogs'));
    }

    public function testMissingLettersReplacedByNumbers()
    {
        $this->markTestSkipped();
        $this->assertFalse(isPangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'));
    }

    public function testPangramWithMixedCaseAndPunctuation()
    {
        $this->markTestSkipped();
        $this->assertTrue(isPangram('\Five quacking Zephyrs jolt my wax bed.\\'));
    }

    public function testPangramWithNonAsciiCharacters()
    {
        $this->markTestSkipped();
        $this->assertTrue(isPangram('Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.'));
    }
}
<?php

function isPangram($str, $enc = 'UTF-8')
{
    static $letters_in_ascii = 26;
    $str = iconv($enc, 'ASCII//TRANSLIT//IGNORE', $str);
    $str = preg_replace('/\PL/u', '', $str);
    $str = strtoupper($str);
    $letters = count(array_count_values(str_split($str)));
    return $letters === $letters_in_ascii;
}

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?

Community comments

See what others have said about this solution
almost 2 years ago
4d47 says

preg_replace and str_split could be replaced with preg_match_all