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

dgeiger's solution

to Food Chain in the Delphi Pascal Track

Published at Sep 10 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

uses
  System.Generics.Collections, System.SysUtils;

type
  TAnimal = (fly, spider, bird, cat, dog, goat, cow, horse);

  TFoodChain = class
    private
      class function AddStanza(Stanza: TAnimal): String;
      class var FAnimals: TDictionary<TAnimal, String>;
      class var FLyric: TDictionary<TAnimal, String>;
      class var FSong: String;

    public
      class function Recite(StartStanza, EndStanza: Integer): string;

  end;

implementation

const
  NewLine     = '\n';
  Preamble    = 'I know an old lady who swallowed a %s.' + NewLine;
  StanzaEnd   = 'I don''t know why she swallowed the fly. Perhaps she''ll die.';
  StanzaIntro = 'She swallowed the %s to catch the %s.\n';
  SpiderIntro = 'She swallowed the %s to catch the %s.\n';
  SpiderLine2 = 'She swallowed the %s to catch the %s that wriggled and jiggled and tickled inside her.\n';
  SongEnd     = 'She''s dead, of course!';

{ TFoodChain }

class function TFoodChain.AddStanza(Stanza: TAnimal): String;
var
  Index: TAnimal;
  ThisAnimal: String;
  PreviousAnimal: String;
begin
  // We start the stanza with the woman following the animal
  Result := Format(Preamble, [FAnimals[Stanza]]) + FLyric[Stanza];

  // The horse is one of two edge cases, so we handle it here
  if Stanza = horse then
    begin
      // If the woman swallows the horse, she dies,
      Result := Result + SongEnd;

      // so we leave early
      Exit;
    end;

  // The fly is the other edge case. All the other animals are swallowed to
  // catch the previous animal
  if Stanza <> Fly then
    // This animal isn't the horse or the fly, so we catch the previous animal(s)
    for Index := Stanza downto Succ(fly) do
      begin
        // Get the name of the current animal
        ThisAnimal := FAnimals[Index];

        // and the name of the previous animal
        PreviousAnimal := FAnimals[Pred(Index)];

        // The spider is a little different than the other animals, and has
        // special lyrics when it is the current or previous animal.

        // Is this the spider?
        if Stanza = spider then
          // Yes, use the spider's lyrics
          Result := Result + Format(SpiderIntro, [ThisAnimal, PreviousAnimal])
        else
          // Is it the bird instead?
          if Index = bird then
            // Yes, use the bird's lyrics about the spider
            Result := Result + Format(SpiderLine2, [ThisAnimal, PreviousAnimal])
          else
            // No, it's a normal animal
            Result := Result + Format(StanzaIntro, [ThisAnimal, PreviousAnimal]);
      end;

  // Return the lyrics for the stanza
  Result := Result + StanzaEnd;
end;

class function TFoodChain.Recite(StartStanza, EndStanza: Integer): string;
var
  Index: TAnimal;
  StartAnimal: TAnimal;
  EndAnimal: TAnimal;
begin
  // Initialize the dictionary of animal names
  FAnimals := TDictionary<TAnimal, String>.Create;
  FAnimals.Add(fly,    'fly');
  FAnimals.Add(spider, 'spider');
  FAnimals.Add(bird,   'bird');
  FAnimals.Add(cat,    'cat');
  FAnimals.Add(dog,    'dog');
  FAnimals.Add(goat,   'goat');
  FAnimals.Add(cow,    'cow');
  FAnimals.Add(horse,  'horse');

  // Initialize the dictionary of animal lyrics
  FLyric   := TDictionary<TAnimal, String>.Create;
  FLyric.Add(fly,    '');
  FLyric.Add(spider, 'It wriggled and jiggled and tickled inside her.' + NewLine);
  FLyric.Add(bird,   'How absurd to swallow a bird!' + NewLine);
  FLyric.Add(cat,    'Imagine that, to swallow a cat!' + NewLine);
  FLyric.Add(dog,    'What a hog, to swallow a dog!' + NewLine);
  FLyric.Add(goat,   'Just opened her throat and swallowed a goat!' + NewLine);
  FLyric.Add(cow,    'I don''t know how she swallowed a cow!' + NewLine);
  FLyric.Add(horse,  '');

  // Set the song to an empty string
  FSong := '';

  // Decrement the starting stanza number to make it zero-based like the
  // enumerated type TAnimal
  Dec(StartStanza);

  // and get the animal for that number.
  StartAnimal := TAnimal(StartStanza);

  // Decrement the starting stanza number to make it zero-based like the
  // enumerated type TAnimal
  Dec(EndStanza);

  // and get the animal for that number.
  EndAnimal := TAnimal(EndStanza);

  // For each animal's stanza
  for Index := StartAnimal to EndAnimal do
    begin
      // We get the stanza and add it to the song
      FSong := FSong + AddStanza(Index);

      // If we aren't at the last stanza, and the newline characters
      if Index < EndAnimal then
        FSong := FSong + NewLine + NewLine;
    end;

  // Return the song lyrics
  Result := FSong;
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?