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

rpottsoh's solution

to Wordy in the Delphi Pascal Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Parse and evaluate simple math word problems returning the answer as an integer.

Iteration 1 β€” Addition

Add two numbers together.

What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

Iteration 2 β€” Subtraction, Multiplication and Division

Now, perform the other three operations.

What is 7 minus 5?

2

What is 6 multiplied by 4?

24

What is 25 divided by 5?

5

Iteration 3 β€” Multiple Operations

Handle a set of operations, in sequence.

Since these are verbal word problems, evaluate the expression from left-to-right, ignoring the typical order of operations.

What is 5 plus 13 plus 6?

24

What is 3 plus 2 multiplied by 3?

15 (i.e. not 9)

Bonus β€” Exponentials

If you'd like, handle exponentials.

What is 2 raised to the 5th power?

32

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

Inspired by one of the generated questions in the Extreme Startup game. https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

uWordyTests.pas

unit uWordyTests;

interface
uses
  DUnitX.TestFramework;
  
const
  CanonicalVersion = '1.1.0';

type

  [TestFixture]
  WordyTests = class(TObject)
  public
//   [Ignore('Comment the "[Ignore]" statement to run the test')]
   [TestCase('addition', 'What is 1 plus 1?,2')]
   procedure Addition(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('more addition', 'What is 53 plus 2?,55')]
   procedure More_Addition(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('addition with negative numbers', 'What is -1 plus -10?,-11')]
   procedure Addition_with_negative_numbers(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('large addition', 'What is 123 plus 45678?,45801')]
   procedure Large_addition(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('subtraction', 'What is 4 minus -12?,16')]
   procedure Subtraction(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('multiplication', 'What is -3 multiplied by 25?,-75')]
   procedure Multiplication(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('division', 'What is 33 divided by -3?,-11')]
   procedure Division(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('multiple additions', 'What is 1 plus 1 plus 1?,3')]
   procedure Multiple_additions(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('addition and subtraction', 'What is 1 plus 5 minus -2?,8')]
   procedure Addition_and_subtraction(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('multiple subtraction', 'What is 20 minus 4 minus 13?,3')]
   procedure Multiple_subtraction(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('subtraction then addition', 'What is 17 minus 6 plus 3?,14')]
   procedure Subtraction_then_addition(const aInput: string; const aExpected: integer);

   [Ignore]
   [TestCase('multiple multiplication', 'What is 2 multiplied by -2 multiplied by 3?,-12')]
   procedure Multiple_multiplication(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('addition and multiplication', 'What is -3 plus 7 multiplied by -2?,-8')]
   procedure Addition_and_multiplication(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('multiple division', 'What is -12 divided by 2 divided by -3?,2')]
   procedure Multiple_division(const aInput: string; aExpected: integer);

   [Ignore]
   [TestCase('unknown operation', 'What is 52 cubed?,Invalid Problem')]
   procedure Unknown_operation(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('Non math question', 'Who is the President of the United States?,Invalid Problem')]
   procedure Non_math_question(const aInput: string; aExpected: string);
  end;

implementation
uses uWordy;

procedure WordyTests.Addition(const aInput: string; const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Addition_and_multiplication(const aInput: string;
  aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Addition_and_subtraction(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Addition_with_negative_numbers(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Division(const aInput: string; aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Large_addition(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.More_Addition(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Multiple_additions(const aInput: string;
  aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Multiple_division(const aInput: string;
  aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Multiple_multiplication(const aInput: string;
  aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Multiple_subtraction(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Multiplication(const aInput: string; aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Subtraction(const aInput: string; aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Subtraction_then_addition(const aInput: string;
  const aExpected: integer);
begin
  Assert.AreEqual(aExpected, TWordy.Answer(aInput));
end;

procedure WordyTests.Unknown_operation(const aInput: string; aExpected: string);
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              TWordy.Answer(aInput);
            end;

  Assert.WillRaiseWithMessage(MyProc, EInvalidProblem, aExpected);
end;

procedure WordyTests.Non_math_question(const aInput: string; aExpected: string);
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              TWordy.Answer(aInput);
            end;

  Assert.WillRaiseWithMessage(MyProc, EInvalidProblem, aExpected);
end;

initialization
  TDUnitX.RegisterTestFixture(WordyTests);
end.
unit uWordy;

interface
uses System.SysUtils, RegularExpressions, System.Generics.Collections;

type
  EInvalidProblem = Class(Exception);

  TWordy = Class
  private
    type
      TParsedProblem = record
        X: integer;
        Operation1: string;
        Y: integer;
        Operation2: string;
        Z: integer;
        constructor create(parsedGroups: TGroupCollection);
      end;
    class var Operations: TDictionary<string, TFunc<integer, integer, integer>>;
    class var VariableAndOperatorGroupsRegex: TRegex;
    class procedure buildOperations;
    class procedure buildVariableAndOperatorGroupsRegex;
    class function ParseProblem(aProblem: string): TParsedProblem;
  public
    class function Answer(aQuestion: string): integer;
  end;

implementation
uses Math;

{ TWordy }

class function TWordy.Answer(aQuestion: string): integer;
var ParsedProblem: TParsedProblem;
    Operation: TFunc<integer, integer, integer>;
    firstPass: integer;
begin
  if not assigned(Operations) then
  begin
    buildOperations;
    buildVariableAndOperatorGroupsRegex;
  end;

  ParsedProblem := ParseProblem(aQuestion);
  Operation := Operations[ParsedProblem.Operation1];
  firstPass := Operation(ParsedProblem.X, ParsedProblem.Y);
  if ParsedProblem.Operation2.IsEmpty then
    result := firstPass
  else
  begin
    Operation := Operations[ParsedProblem.Operation2];
    result := Operation(firstPass, ParsedProblem.Z);
  end;
end;

class procedure TWordy.BuildOperations;
begin
  Operations := TDictionary<string, TFunc<integer, integer, integer>>.Create;
  Operations.Add('plus',
    function(X: integer; Y: integer): integer
    begin
      result := X + Y;
    end);

  Operations.Add('minus',
    function(X: integer; Y: integer): integer
    begin
      result := X - Y;
    end);

  Operations.Add('multiplied by',
    function(X: integer; Y: integer): integer
    begin
      result := X * Y;
    end);

  Operations.Add('divided by',
    function(X: integer; Y: integer): integer
    begin
      result := X div Y;
    end);
end;

class procedure TWordy.buildVariableAndOperatorGroupsRegex;
begin
  VariableAndOperatorGroupsRegex := TRegex.Create(format('%0:s %1:s %0:s\s*%1:s*\s*%0:s*',
    ['(-?\d+)','(plus|minus|multiplied by|divided by)']));
end;

class function TWordy.ParseProblem(aProblem: string): TParsedProblem;
var Match: TMatch;
begin
  Match := VariableAndOperatorGroupsRegex.Match(aProblem);
  if Match.Groups.Count = 0 then
    raise EInvalidProblem.Create('Invalid Problem');

  result := TParsedProblem.create(Match.Groups);
end;

{ TWordy.TParsedProblem }

constructor TWordy.TParsedProblem.create(parsedGroups: TGroupCollection);
var i: integer;
    parsedItems: TList<string>;
    parsedItemsArray: TArray<string>;
begin
  parsedItems := TList<string>.Create;
  for i := 1 to parsedGroups.Count - 1 do
  begin
    if parsedGroups[i].Success then
      parsedItems.Add(parsedGroups[i].Value)
    else
      parsedItems.Add('');
  end;

  parsedItemsArray := parsedItems.ToArray;
  Setlength(parsedItemsArray, 5);

  X := parsedItemsArray[0].ToInteger;
  Operation1 := parsedItemsArray[1];
  Y := parsedItemsArray[2].ToInteger;
  Operation2 := parsedItemsArray[3];
  if parsedItemsArray[4] <> '' then
    Z := parsedItemsArray[4].ToInteger
  else
    Z := 0;
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?