Avatar of rajesh-varatharajan

rajesh-varatharajan's solution

to Pig Latin in the PHP Track

Published at Sep 04 2019 · 0 comments
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

class PigLatinTest extends PHPUnit\Framework\TestCase
{
    public static function setUpBeforeClass() : void
    {
        require_once 'pig-latin.php';
    }

    public function testWordBeginningWithP() : void
    {
        $this->assertEquals("igpay", translate("pig"));
    }

    public function testWordBeginningWithK() : void
    {
        $this->assertEquals("oalakay", translate("koala"));
    }

    public function testWordBeginningWithY() : void
    {
        $this->assertEquals("ellowyay", translate("yellow"));
    }

    public function testWordBeginningWithX() : void
    {
        $this->assertEquals("enonxay", translate("xenon"));
    }

    public function testWordBeginningWithA() : void
    {
        $this->assertEquals("appleay", translate("apple"));
    }

    public function testWordBeginningWithE() : void
    {
        $this->assertEquals("earay", translate("ear"));
    }

    public function testWordBeginningWithI() : void
    {
        $this->assertEquals("iglooay", translate("igloo"));
    }

    public function testWordBeginningWithO() : void
    {
        $this->assertEquals("objectay", translate("object"));
    }

    public function testWordBeginningWithU() : void
    {
        $this->assertEquals("underay", translate("under"));
    }

    public function testWordBeginningVowelFollowedByQu() : void
    {
        $this->assertEquals("equalay", translate("equal"));
    }

    public function testWordBeginningWithQWithoutAFollowingU() : void
    {
        $this->assertEquals("atqay", translate("qat"));
    }

    public function testWordBeginningWithCh() : void
    {
        $this->assertEquals("airchay", translate("chair"));
    }

    public function testWordBeginningWithQu() : void
    {
        $this->assertEquals("eenquay", translate("queen"));
    }

    public function testWordBeginningWithQuAndAPrecedingConsonant() : void
    {
        $this->assertEquals("aresquay", translate("square"));
    }

    public function testWordBeginningWithTh() : void
    {
        $this->assertEquals("erapythay", translate("therapy"));
    }

    public function testWordBeginningWithThr() : void
    {
        $this->assertEquals("ushthray", translate("thrush"));
    }

    public function testWordBeginningWithSch() : void
    {
        $this->assertEquals("oolschay", translate("school"));
    }

    public function testWordBeginningWithYt() : void
    {
        $this->assertEquals("yttriaay", translate("yttria"));
    }

    public function testWordBeginningWithXr() : void
    {
        $this->assertEquals("xrayay", translate("xray"));
    }

    public function testAWholePhrase() : void
    {
        $this->assertEquals("ickquay astfay unray", translate("quick fast run"));
    }
}
<?php
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
const PIG_LATIN_AY = 'ay';
const PECULIAR_WORDS = ['squ', 'qu', 'thr', 'th', 'sch'];

function translate (string $words) {
    $explodeWords = explode(' ', $words);

    $result = '';
    foreach ($explodeWords as $word) {
        $result .= translateToPigLatin($word) . ' ';
    }

    return rtrim($result);
}

function translateToPigLatin(string $word) : string
{
    if (isWordStartWithVowels($word)) {
        return $word . PIG_LATIN_AY;
    }

    if (isWordBeginningWithSpecificCase($word)) {
        return wordBeginningWithSpecificCase($word);
    }

    if (strpos($word, 'yt') !== false || strpos($word, 'xr') !== false) {
        return $word . PIG_LATIN_AY;
    }

    $chars = str_split($word);
    $result = handleWordContainVowel($chars);
    if ($result) {
        return $result;
    }

    return $word . PIG_LATIN_AY;
}

function wordBeginningWithSpecificCase(string $word) : string
{
    $matchedWord = null;
    foreach (PECULIAR_WORDS as $w) {
        if (stripos($word, $w) !== false) {
            $matchedWord = $w;
            break;
        }
    }

    if ($matchedWord != null) {
        $len = strlen($matchedWord);
        $firstPart = substr($word, 0, $len);
        $lastPart = substr($word, $len, strlen($word) - 1);
        return $lastPart . $firstPart . PIG_LATIN_AY;
    }

   return null;
}

function isWordBeginningWithSpecificCase($word) : bool
{
    $matchedWord = null;
    foreach (PECULIAR_WORDS as $w) {
        if (stripos($word, $w) !== false) {
            return true;
        }
    }

    return false;
}

function handleWordContainVowel(array $chars)
{
    $lastPart = '';
    $firstPart = '';
    foreach ($chars as $k => $c) {
        if (in_array($c, VOWELS)) {
            $firstPart = array_splice($chars, $k, count(VOWELS));
            break;
        } else {
            $lastPart .= $c;
        }
    }

    if ($firstPart) {
        return implode('', $firstPart) . $lastPart . PIG_LATIN_AY;
    }

    return null;
}

function isWordStartWithVowels(string $word)
{
    if (in_array(substr($word, 0, 1), VOWELS)) {
        return true;
    }

    return false;
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?