Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Isogram in the PHP Track

Instructions
Test suite
Solution

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

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 isogram/isogram_test.php
    

Source

Wikipedia https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

isogram_test.php

<?php

require "isogram.php";

class IsogramTest extends PHPUnit\Framework\TestCase
{
    public function testIsogram()
    {
        $this->assertTrue(isIsogram('duplicates'));
    }

    public function testNotIsogram()
    {
        $this->markTestSkipped();
        $this->assertFalse(isIsogram('eleven'));
    }

    public function testMediumLongIsogram()
    {
        $this->markTestSkipped();
        $this->assertTrue(isIsogram('subdermatoglyphic'));
    }

    public function testCaseInsensitive()
    {
        $this->markTestSkipped();
        $this->assertFalse(isIsogram('Alphabet'));
    }

    public function testIsogramWithHyphen()
    {
        $this->markTestSkipped();
        $this->assertTrue(isIsogram('thumbscrew-japingly'));
    }

    public function testIgnoresMultipleHyphens()
    {
        $this->markTestSkipped();
        $this->assertTrue(isIsogram('Hjelmqvist-Gryb-Zock-Pfund-Wax'));
    }

    public function testWorksWithGermanLetters()
    {
        $this->markTestSkipped();
        $this->assertTrue(isIsogram('Heizölrückstoßabdämpfung'));
    }

    public function testIgnoresSpaces()
    {
        $this->markTestSkipped();
        $this->assertFalse(isIsogram('the quick brown fox'));
    }

    public function testIgnoresSpaces2()
    {
        $this->markTestSkipped();
        $this->assertTrue(isIsogram('Emily Jung Schwartzkopf'));
    }

    public function testDuplicateAccentedLetters()
    {
        $this->markTestSkipped();
        $this->assertFalse(isIsogram('éléphant'));
    }
}
<?php

function isIsogram($str, $enc = 'UTF-8')
{
    preg_match_all('/\pL/u', mb_strtoupper($str), $results);
    $letter_counts = array_count_values($results[0]);
    return max($letter_counts) === 1;
}

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?