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

Neftali's solution

to Proverb in the Delphi Pascal Track

Published at Jan 24 2020 · 2 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

uses
  System.Generics.Collections, System.Classes;

type
  TStringsArray = TArray<string>;
  TArrayHelper = class helper for TStrings
    function recite:TArray<string>;
  end;

function Proverb(Inputs:TArray<string>):TStrings;

implementation

uses
  System.Sysutils;

function Proverb(Inputs:TArray<string>):TStrings;
const
  LAST_LINE = 'And all for the want of a %s.';
  ALL_LINES = 'For want of a %s the %s was lost.';
var
  i:Integer;
begin
  Result := TStringList.Create;
  if Length(Inputs) = 0 then
    Exit;

  for i := 1 to (Length(Inputs) - 1) do
    Result.Add(Format(ALL_LINES, [Inputs[i-1], Inputs[i]]));
  Result.Add(Format(LAST_LINE,[Inputs[0]]));
end;

{ TArrayHelper }

function TArrayHelper.recite: TArray<string>;
var
  l:TList<String>;
  i:Integer;
begin
  l := TList<string>.Create;
  try
    for i := 0 to (Self.Count - 1) do
      l.Add(Self[i]);
    Result := l.ToArray;
  finally
    FreeAndNil(l);
  end;
end;

end.

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of Neftali

I have use Class helper instead a interfaces.

Avatar of Neftali

I have use Class helper instead a interfaces.

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?