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

Greycoat21's solution

to Food Chain in the Delphi Pascal Track

Published at Feb 02 2020 · 0 comments
Instructions
Test suite
Solution

Generate the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'.

While you could copy/paste the lyrics, or read them from a file, this problem is much more interesting if you approach it algorithmically.

This is a cumulative song of unknown origin.

This is one of many common variants.

I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
She's dead, of course!

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

Wikipedia http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

uFoodChainTests.pas

unit uFoodChainTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '2.1.0.1';

type

  [TestFixture]
  TFoodChainTest = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure fly;

    [Test]
    [Ignore]
    procedure spider;

    [Test]
    [Ignore]
    procedure bird;

    [Test]
    [Ignore]
    procedure cat;

    [Test]
    [Ignore]
    procedure dog;

    [Test]
    [Ignore]
    procedure goat;

    [Test]
    [Ignore]
    procedure cow;

    [Test]
    [Ignore]
    procedure horse;

    [Test]
    [Ignore]
    procedure multiple_verses;

    [Test]
    [Ignore]
    procedure full_song;
  end;

implementation

uses
  uFoodChain;

procedure TFoodChainTest.spider;
begin
  Assert.AreEqual('I know an old lady who swallowed a spider.' + '\n' +
            'It wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(2, 2));
end;

procedure TFoodChainTest.bird;
begin
  Assert.AreEqual('I know an old lady who swallowed a bird.' + '\n' +
            'How absurd to swallow a bird!' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(3, 3));
end;

procedure TFoodChainTest.cat;
begin
   Assert.AreEqual('I know an old lady who swallowed a cat.' + '\n' +
            'Imagine that, to swallow a cat!' + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(4, 4));
end;

procedure TFoodChainTest.cow;
begin
Assert.AreEqual('I know an old lady who swallowed a cow.' + '\n' +
            'I don''t know how she swallowed a cow!' + '\n' +
            'She swallowed the cow to catch the goat.' + '\n' +
            'She swallowed the goat to catch the dog.' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(7, 7));
end;

procedure TFoodChainTest.dog;
begin
   Assert.AreEqual('I know an old lady who swallowed a dog.' + '\n' +
            'What a hog, to swallow a dog!' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(5, 5));
end;

procedure TFoodChainTest.fly;
begin
  Assert.AreEqual('I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(1, 1));
end;

procedure TFoodChainTest.full_song;
begin
  Assert.AreEqual('I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a spider.' + '\n' +
            'It wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a bird.' + '\n' +
            'How absurd to swallow a bird!' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a cat.' + '\n' +
            'Imagine that, to swallow a cat!' + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a dog.' + '\n' +
            'What a hog, to swallow a dog!' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a goat.' + '\n' +
            'Just opened her throat and swallowed a goat!' + '\n' +
            'She swallowed the goat to catch the dog.' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a cow.' + '\n' +
            'I don''t know how she swallowed a cow!' + '\n' +
            'She swallowed the cow to catch the goat.' + '\n' +
            'She swallowed the goat to catch the dog.' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a horse.' + '\n' +
            'She''s dead, of course!',
    TFoodChain.Recite(1, 8));
end;

procedure TFoodChainTest.goat;
begin
Assert.AreEqual('I know an old lady who swallowed a goat.' + '\n' +
            'Just opened her throat and swallowed a goat!' + '\n' +
            'She swallowed the goat to catch the dog.' + '\n' +
            'She swallowed the dog to catch the cat.'  + '\n' +
            'She swallowed the cat to catch the bird.' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(6, 6));
end;

procedure TFoodChainTest.horse;
begin
  Assert.AreEqual('I know an old lady who swallowed a horse.' + '\n' +
            'She''s dead, of course!',
    TFoodChain.Recite(8, 8));
end;

procedure TFoodChainTest.multiple_verses;
begin
 Assert.AreEqual('I know an old lady who swallowed a fly.\nI don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a spider.' + '\n' +
            'It wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.' + '\n' +
            '\n' +
            'I know an old lady who swallowed a bird.' + '\n' +
            'How absurd to swallow a bird!' + '\n' +
            'She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.' + '\n' +
            'She swallowed the spider to catch the fly.' + '\n' +
            'I don''t know why she swallowed the fly. Perhaps she''ll die.',
    TFoodChain.Recite(1, 3));
end;

initialization
  TDUnitX.RegisterTestFixture(TFoodChainTest);
end.
unit uFoodChain;

interface

type
  TFoodChain = class
    class function Recite(StartVerse, EndVerse: Integer): string; overload;
    strict private class function Recite(Verse: Integer): string; overload;
  end;

implementation

{ TFoodChain }

type
  TCritterInfo = record
    Name, InitialComment, SeriesComment: string;
    IsPoisonous: Boolean;
  end;
  TCritter = (fly, spider, bird, cat, dog, goat, cow, horse);

const
  Critter: array [TCritter] of TCritterInfo = (
    (Name: 'fly';
     InitialComment: 'I don''t know why she swallowed the fly. Perhaps she''ll die.';
     SeriesComment: '.\nI don''t know why she swallowed the fly. Perhaps she''ll die'),
    (Name: 'spider';
     InitialComment: 'It wriggled and jiggled and tickled inside her.';
     SeriesComment: ' that wriggled and jiggled and tickled inside her'),
    (Name: 'bird';
     InitialComment: 'How absurd to swallow a bird!'),
    (Name: 'cat';
     InitialComment: 'Imagine that, to swallow a cat!'),
    (Name: 'dog';
     InitialComment: 'What a hog, to swallow a dog!'),
    (Name: 'goat';
     InitialComment: 'Just opened her throat and swallowed a goat!'),
    (Name: 'cow';
     InitialComment: 'I don''t know how she swallowed a cow!'),
    (Name: 'horse';
     InitialComment: 'She''s dead, of course!';
     IsPoisonous: True)
  );

class function TFoodChain.Recite(Verse: Integer): string;
begin
  var Animal := Critter[TCritter(Verse)];
  Result := 'I know an old lady who swallowed a ' + Animal.Name + '.'
    + '\n' + Animal.InitialComment;

  if Animal.IsPoisonous then
    Exit;

  if Verse > 0 then
    for var i := Verse downto 1 do begin
      Animal         := Critter[TCritter(i)];
      var PrevAnimal := Critter[TCritter(i-1)];
      Result := Result + '\n' + 'She swallowed the ' + Animal.Name
        + ' to catch the ' + PrevAnimal.Name
        + PrevAnimal.SeriesComment + '.';
    end;
end;

class function TFoodChain.Recite(StartVerse, EndVerse: Integer): string;
const
  VerseSeparator = '\n\n';
begin
  for var i := StartVerse to EndVerse do begin

    if i <> StartVerse then
      Result := Result + VerseSeparator;

    Result := Result + Recite(i-1);

  end;
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?