Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Pig Latin in the PHP Track

Instructions
Test suite
Solution

Implement a program that translates from English to Pig Latin.

Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below), but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.

  • Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
  • Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.

There are a few more rules for edge cases, and there are regional variants too.

See http://en.wikipedia.org/wiki/Pig_latin for more details.

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 pig-latin/pig-latin_test.php
    

Source

The Pig Latin exercise at Test First Teaching by Ultrasaurus https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

Submitting Incomplete Solutions

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

pig-latin_test.php

<?php
require_once "pig-latin.php";

class PigLatinTest extends PHPUnit\Framework\TestCase
{
    public function testWordBeginningWithP()
    {
        $this->assertEquals("igpay", translate("pig"));
    }

    public function testWordBeginningWithK()
    {
        $this->markTestSkipped();

        $this->assertEquals("oalakay", translate("koala"));
    }

    public function testWordBeginningWithY()
    {
        $this->markTestSkipped();

        $this->assertEquals("ellowyay", translate("yellow"));
    }

    public function testWordBeginningWithX()
    {
        $this->markTestSkipped();

        $this->assertEquals("enonxay", translate("xenon"));
    }

    public function testWordBeginningWithA()
    {
        $this->markTestSkipped();

        $this->assertEquals("appleay", translate("apple"));
    }

    public function testWordBeginningWithE()
    {
        $this->markTestSkipped();

        $this->assertEquals("earay", translate("ear"));
    }

    public function testWordBeginningWithI()
    {
        $this->markTestSkipped();

        $this->assertEquals("iglooay", translate("igloo"));
    }

    public function testWordBeginningWithO()
    {
        $this->markTestSkipped();

        $this->assertEquals("objectay", translate("object"));
    }

    public function testWordBeginningWithU()
    {
        $this->markTestSkipped();

        $this->assertEquals("underay", translate("under"));
    }

    public function testWordBeginningVowelFollowedByQu()
    {
        $this->markTestSkipped();

        $this->assertEquals("equalay", translate("equal"));
    }


    public function testWordBeginningWithQWithoutAFollowingU()
    {
        $this->markTestSkipped();

        $this->assertEquals("atqay", translate("qat"));
    }


    public function testWordBeginningWithCh()
    {
        $this->markTestSkipped();

        $this->assertEquals("airchay", translate("chair"));
    }

    public function testWordBeginningWithQu()
    {
        $this->markTestSkipped();

        $this->assertEquals("eenquay", translate("queen"));
    }

    public function testWordBeginningWithQuAndAPrecedingConsonant()
    {
        $this->markTestSkipped();

        $this->assertEquals("aresquay", translate("square"));
    }

    public function testWordBeginningWithTh()
    {
        $this->markTestSkipped();

        $this->assertEquals("erapythay", translate("therapy"));
    }

    public function testWordBeginningWithThr()
    {
        $this->markTestSkipped();

        $this->assertEquals("ushthray", translate("thrush"));
    }

    public function testWordBeginningWithSch()
    {
        $this->markTestSkipped();

        $this->assertEquals("oolschay", translate("school"));
    }

    public function testWordBeginningWithYt()
    {
        $this->markTestSkipped();

        $this->assertEquals("yttriaay", translate("yttria"));
    }

    public function testWordBeginningWithXr()
    {
        $this->markTestSkipped();

        $this->assertEquals("xrayay", translate("xray"));
    }

    public function testAWholePhrase()
    {
        $this->markTestSkipped();

        $this->assertEquals("ickquay astfay unray", translate("quick fast run"));
    }
}
<?php


function translate($phrase)
{
    // Splitting on space for simplicity
    // but should take care of comma, etc
    return implode(' ', array_map(translate_pigword, explode(' ', $phrase)));
}

function translate_pigword($word) {
    if (preg_match("/^(?:[aeiou]|y[^aeiou])/i", $word) || preg_match('/ay$/i', $word)) {
        return $word . 'ay';
    } else {
        preg_match("/(?:qu|[^aeiou])*/i", $word, $matches);
        $len = strlen($matches[0]);
        return substr($word, $len) . substr($word, 0, $len) . 'ay';
    }
}

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?