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

Greycoat21's solution

to Pig Latin in the Delphi Pascal Track

Published at Mar 15 2020 · 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. 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 may request help from a mentor.

uPigLatinTests.pas

unit uPigLatinTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.2.0.1';

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
  public
    class function Translate(Phrase: string): string;
  private
    class function Convert(Word: string): string;
  end;

implementation

uses
  SysUtils;

{ TPigLatin }

const
  Letter = ['a'..'z'];
  Vowel = ['a', 'e', 'i', 'o', 'u'];
  Consonant = Letter - Vowel;
  Vowelish = ['x', 'y'];

class function TPigLatin.Convert(Word: string): string;
const
  PigTail = 'ay';
var
  Int: Integer;
  Chr, PrvChr: Char;
begin
  Int := 1;
  Chr := Word[Int];
  if CharInSet(Chr, Vowel)
  or (CharInSet(Chr, Vowelish)
    and CharInSet(Word[2], Consonant))
  then
    Exit(Word + PigTail);

  Result := Word;
  while not CharInSet(Chr, Vowel)
  or (CharInSet(PrvChr, ['q']) and CharInSet(Chr, ['u']))
  do begin

    if (CharInSet(PrvChr, Consonant - ['y']) and CharInSet(Chr, ['y'])) then
      Break;

    Result := Result.Replace(Chr, '', [rfIgnoreCase]);
    Result := Result + Chr;

    PrvChr := Chr;
    Inc(Int);
    Chr := Word[Int];

  end;

  Result := Result + PigTail;

end;

class function TPigLatin.Translate(Phrase: string): string;
var
  Word: string;
  Translation: array of string;
begin
  Translation := [];
  for Word in Phrase.Split([' ']) do
    Translation := Translation + [Convert(Word)];

  Result := string.Join(' ', Translation);
end;

end.

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?