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

dgeiger's solution

to Proverb in the Delphi Pascal Track

Published at Aug 30 2020 · 0 comments
Instructions
Test suite
Solution

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb. For example, given the list ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], you will output the full text of this proverbial rhyme:

For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.

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

Submitting Incomplete Solutions

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

uProverbTests.pas

unit uProverbTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.1.0';

type

  [TestFixture]
  ProverbTest = class(TObject)
  private
    procedure CompareExpectedAndActual(aExpected, aActual: array of string);
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure zero_pieces;

    [Test]
    [Ignore]
    procedure one_piece;

    [Test]
    [Ignore]
    procedure two_pieces;

    [Test]
    [Ignore]
    procedure three_pieces;

    [Test]
    [Ignore]
    procedure full_proverb;

    [Test]
    [Ignore]
    procedure four_pieces_modernized;
  end;

implementation
uses uProverb;

{ ProverbTest }

procedure ProverbTest.CompareExpectedAndActual(aExpected,
  aActual: array of string);
var
  i: integer;
begin
  Assert.AreEqual(length(aExpected), length(aActual));
  for i := Low(aExpected) to High(aExpected) do
    Assert.AreEqual(aExpected[i], aActual[i]);
end;

procedure ProverbTest.four_pieces_modernized;
var
  Expected: Array of string;
begin
  SetLength(Expected, 4);
  Expected [0] := 'For want of a pin the gun was lost.';
  Expected [1] := 'For want of a gun the soldier was lost.';
  Expected [2] := 'For want of a soldier the battle was lost.';
  Expected [3] := 'And all for the want of a pin.';
  CompareExpectedAndActual(Expected, Proverb(['pin','gun','soldier','battle']).recite);
end;

procedure ProverbTest.full_proverb;
var
  Expected: Array of string;
begin
  SetLength(Expected, 7);
  Expected[0] := 'For want of a nail the shoe was lost.';
  Expected[1] := 'For want of a shoe the horse was lost.';
  Expected[2] := 'For want of a horse the rider was lost.';
  Expected[3] := 'For want of a rider the message was lost.';
  Expected[4] := 'For want of a message the battle was lost.';
  Expected[5] := 'For want of a battle the kingdom was lost.';
  Expected[6] := 'And all for the want of a nail.';
  CompareExpectedAndActual(Expected, Proverb(['nail','shoe','horse','rider','message','battle','kingdom']).recite);
end;

procedure ProverbTest.one_piece;
var
  Expected: Array of string;
begin
  SetLength(Expected, 1);
  Expected[0] := 'And all for the want of a nail.';
  CompareExpectedAndActual(Expected, Proverb(['nail']).recite);
end;

procedure ProverbTest.three_pieces;
var
  Expected: Array of string;
begin
  SetLength(Expected, 3);
  Expected[0] := 'For want of a nail the shoe was lost.';
  Expected[1] := 'For want of a shoe the horse was lost.';
  Expected[2] := 'And all for the want of a nail.';
  CompareExpectedAndActual(Expected, Proverb(['nail','shoe','horse']).recite);
end;

procedure ProverbTest.two_pieces;
var
  Expected: Array of string;
begin
  SetLength(Expected, 2);
  Expected[0] := 'For want of a nail the shoe was lost.';
  Expected[1] := 'And all for the want of a nail.';
  CompareExpectedAndActual(Expected, Proverb(['nail','shoe']).recite);
end;

procedure ProverbTest.zero_pieces;
var
  Expected: Array of string;
begin
  SetLength(Expected, 0);
  CompareExpectedAndActual(Expected, Proverb([]).recite);
end;

initialization
  TDUnitX.RegisterTestFixture(ProverbTest);
end.
unit uProverb;

interface

type
  TProverb = class
    private
      FProverb: TArray<String>;
      constructor Create(Words: TArray<string>);

    public
      function recite: TArray<String>;

  end;

  function Proverb(Words: TArray<string>): TProverb;

implementation

{ Proverb }

constructor TProverb.Create(Words: TArray<string>);
var
  Index: Integer;
  WordCount: Integer;
begin
  // For every word, there will be one line in the proverb, so let's set the length
  WordCount := Length(Words);
  SetLength(FProverb, WordCount);

  // If there are words to create the proverb from, let's create it
  if WordCount > 0 then
    begin
      // We'll decrement WordCount because a TArray<> is zero-based
      Dec(WordCount);

      // For each word, we'll create a line of the proverb. The first
      // WordCount - 1 lines follow one formula, while the last line follows
      // a different formula.
      for Index := 0 to WordCount - 1 do
        // Create one of the WordCount - 1 lines by using two Words - the
        // first Word is at position Index, and the second is at Index + 1.
        FProverb[Index] := 'For want of a ' + Words[index] + ' the ' + Words[index + 1] + ' was lost.';

      // Now, we create the final line, use the first Word, Word[0].
      FProverb[WordCount] := 'And all for the want of a ' + Words[0] + '.';
    end;
end;

function TProverb.recite: TArray<String>;
begin
  // Return the TArray<> containing the text of the proverb.
  Result := FProverb;
end;

function Proverb(Words: TArray<string>): TProverb;
begin
  // Create a new TProverb and return it
  Result := TProverb.Create(Words);
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?