Avatar of csanjuanp

csanjuanp's solution

to Pangram in the PHP Track

Published at Sep 27 2019 · 0 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 may contain non-ASCII symbols, but it is only necessary to check whether each letter in the English alphabet is present.

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

class PangramTest extends PHPUnit\Framework\TestCase
{
    public static function setUpBeforeClass() : void
    {
        require_once 'pangram.php';
    }

    public function testSentenceEmpty() : void
    {
        $this->assertFalse(isPangram(''));
    }

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

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

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

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

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

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

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

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

    public function testMissingLetterReplacedWithUpperCaseCharacter() : void
    {
        $this->assertFalse(isPangram("Tthe quick brown fo jumps over the lazy dog"));
    }
}
<?php

function isPangram($sentence){
    $sentence = strtolower($sentence);
    $num_letras = 26;
    $datos = [];
    for ($inx=0; $inx<strlen($sentence);$inx++){
            if (preg_match("/^[a-z]$/", $sentence[$inx])) $datos[$sentence[$inx]] = 0;
    }

    return count($datos) == $num_letras;
 }

What can you learn from this solution?

A huge amount can be learned 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 you could read more about to improve your understanding?