🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of amoradell

amoradell's solution

to Pig Latin in the Delphi Pascal Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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. Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay").
  • 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. Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay").
  • Rule 3: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay").
  • Rule 4: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").

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.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

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.

uTestPigLatin.pas

unit uTestPigLatin;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.2.0';

type
  [TestFixture]
  PigLatinTest = class(TObject)
  published
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure Word_beginning_with_a;

    [Ignore]
    procedure Word_beginning_with_e;

    [Ignore]
    procedure Word_beginning_with_i;

    [Ignore]
    procedure Word_beginning_with_o;

    [Ignore]
    procedure Word_beginning_with_u;

    [Ignore]
    procedure Word_beginning_with_a_vowel_and_followed_by_a_qu;

    [Ignore]
    procedure Word_beginning_with_p;

    [Ignore]
    procedure Word_beginning_with_k;

    [Ignore]
    procedure Word_beginning_with_x;

    [Ignore]
    procedure Word_beginning_with_q_without_a_following_u;

    [Ignore]
    procedure Word_beginning_with_ch;

    [Ignore]
    procedure Word_beginning_with_qu;

    [Ignore]
    procedure Word_beginning_with_qu_and_a_preceding_consonant;

    [Ignore]
    procedure Word_beginning_with_th;

    [Ignore]
    procedure Word_beginning_with_thr;

    [Ignore]
    procedure Word_beginning_with_sch;

    [Ignore]
    procedure Word_beginning_with_yt;

    [Ignore]
    procedure Word_beginning_with_xr;

    [Ignore]
    procedure y_is_treated_like_a_consonant_at_the_beginning_of_a_word;

    [Ignore]
    procedure y_is_treated_like_a_vowel_at_the_end_of_a_consonant_cluster;

    [Ignore]
    procedure y_as_second_letter_in_two_letter_word;

    [Ignore]
    procedure A_whole_phrase;
  end;

implementation
uses uPigLatin;


{ PigLatinTest }

procedure PigLatinTest.A_whole_phrase;
begin
  Assert.AreEqual('ickquay astfay unray', TPigLatin.Translate('quick fast run'));
end;

procedure PigLatinTest.Word_beginning_with_a;
begin
  Assert.AreEqual('appleay', TPigLatin.Translate('apple'));
end;

procedure PigLatinTest.Word_beginning_with_a_vowel_and_followed_by_a_qu;
begin
  Assert.AreEqual('equalay', TPigLatin.Translate('equal'));
end;

procedure PigLatinTest.Word_beginning_with_ch;
begin
  Assert.AreEqual('airchay', TPigLatin.Translate('chair'));
end;

procedure PigLatinTest.Word_beginning_with_e;
begin
  Assert.AreEqual('earay', TPigLatin.Translate('ear'));
end;

procedure PigLatinTest.Word_beginning_with_i;
begin
  Assert.AreEqual('iglooay', TPigLatin.Translate('igloo'));
end;

procedure PigLatinTest.Word_beginning_with_k;
begin
  Assert.AreEqual('oalakay', TPigLatin.Translate('koala'));
end;

procedure PigLatinTest.Word_beginning_with_o;
begin
  Assert.AreEqual('objectay', TPigLatin.Translate('object'));
end;

procedure PigLatinTest.Word_beginning_with_p;
begin
  Assert.AreEqual('igpay', TPigLatin.Translate('pig'));
end;

procedure PigLatinTest.Word_beginning_with_qu;
begin
  Assert.AreEqual('eenquay', TPigLatin.Translate('queen'));
end;

procedure PigLatinTest.Word_beginning_with_qu_and_a_preceding_consonant;
begin
  Assert.AreEqual('aresquay', TPigLatin.Translate('square'));
end;

procedure PigLatinTest.Word_beginning_with_q_without_a_following_u;
begin
  Assert.AreEqual('atqay', TPigLatin.Translate('qat'));
end;

procedure PigLatinTest.Word_beginning_with_sch;
begin
  Assert.AreEqual('oolschay', TPigLatin.Translate('school'));
end;

procedure PigLatinTest.Word_beginning_with_th;
begin
  Assert.AreEqual('erapythay', TPigLatin.Translate('therapy'));
end;

procedure PigLatinTest.Word_beginning_with_thr;
begin
  Assert.AreEqual('ushthray', TPigLatin.Translate('thrush'));
end;

procedure PigLatinTest.Word_beginning_with_u;
begin
  Assert.AreEqual('underay', TPigLatin.Translate('under'));
end;

procedure PigLatinTest.Word_beginning_with_x;
begin
  Assert.AreEqual('enonxay', TPigLatin.Translate('xenon'));
end;

procedure PigLatinTest.Word_beginning_with_xr;
begin
  Assert.AreEqual('xrayay', TPigLatin.Translate('xray'));
end;

procedure PigLatinTest.y_as_second_letter_in_two_letter_word;
begin
  Assert.AreEqual('ymay', TPigLatin.Translate('my'));
end;

procedure PigLatinTest.y_is_treated_like_a_consonant_at_the_beginning_of_a_word;
begin
  Assert.AreEqual('ellowyay', TPigLatin.Translate('yellow'));
end;

procedure PigLatinTest.y_is_treated_like_a_vowel_at_the_end_of_a_consonant_cluster;
begin
  Assert.AreEqual('ythmrhay', TPigLatin.Translate('rhythm'));
end;

procedure PigLatinTest.Word_beginning_with_yt;
begin
  Assert.AreEqual('yttriaay', TPigLatin.Translate('yttria'));
end;

initialization
  TDUnitX.RegisterTestFixture(PigLatinTest);
end.
unit uPigLatin;
interface
type
  TPigLatin = class(TObject)
    class function Translate(sWord:string):string;
  end;

implementation
uses Classes, System.Sysutils;
{
Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
        xr and yt sound like e
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.
}

const
  VowelsB : Set of AnsiChar = ['a','e','i','o','u'];
  VowelsE : Set of AnsiChar = ['a','e','i','o','u','y'];
  VowelsQ : Set of AnsiChar = ['a','e','i','o'];

class function TPigLatin.Translate(sWord:string):string;
var sPig, sVowels, sConsonants, sBegin, sSpecial:string;
  i : integer;
  VowelsSet:Set of AnsiChar;
  slSpecialQ:TStringlist;
  sWords:TArray<String>;
begin
  sWords := sWord.Split([' ']);
  result := '';
  slSpecialQ := TStringlist.create;
  try
    slSpecialQ.add('qu');
    slSpecialQ.add('sq');

    for sPig in sWords do
    begin
      sVowels := '';
      sConsonants := '';
      if ((CharInSet(sPig[1] ,VowelsB)) or (copy(sWord,1,2)='xr') or(copy(sWord,1,2)='yt')) then
        result := result + sPig + 'ay'
      else
      begin
        VowelsSet := VowelsE;
        sBegin := copy(sPig,1,2);
        sConsonants := sPig[1];
        for sSpecial in slSpecialQ do
        begin
          if (sSpecial = sBegin) then
          begin
            VowelsSet:= VowelsQ;
            break;
          end;
        end;

        i := 2;
        while not (CharInSet(sPig[i] , VowelsSet)) do
        begin
          sConsonants := sConsonants + sPig[i];
          inc(i);
        end;
        sVowels := copy(sPig, length(sConsonants)+1, maxint);
        result := result + sVowels + sConsonants + 'ay';
      end;
      result := result + ' ';
    end;
  finally
    slSpecialQ.Free;
  end;
  result := trim(result);
end;

end.

Community comments

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

Congrats on completing this exercise. Think about how you could refactor this to make it easier for another programmer to follow. One suggestion I have would be to refactor so that you have a method that deals with translating just single words instead of one method that does the whole input string in single swoop.

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?