ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# 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.
``````

## Testing

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

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`.

## 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' +
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' +
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 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 }

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;

// Initialize the dictionary of animal lyrics
FLyric   := TDictionary<TAnimal, String>.Create;
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);

// 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

// 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.``````