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

dgeiger's solution

to Wordy in the Delphi Pascal Track

Published at Sep 08 2020 · 0 comments
Instructions
Test suite
Solution

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

Iteration 0 β€” Numbers

Problems with no operations simply evaluate to the number given.

What is 5?

Evaluates to 5.

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)

Iteration 4 β€” Errors

The parser should reject:

  • Unsupported operations ("What is 52 cubed?")
  • Non-math questions ("Who is the President of the United States")
  • Word problems with invalid syntax ("What is 1 plus plus 2?")

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 may request help from a mentor.

uWordyTests.pas

unit uWordyTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.5.0';

type

  [TestFixture]
  WordyTests = class(TObject)
  public
//   [Ignore('Comment the "[Ignore]" statement to run the test')]
   [TestCase('a number', 'What is 5?,5')]
   procedure just_a_number(const aInput: string; const aExpected: integer);

   [Ignore]
   [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?,unknown operation')]
   procedure Unknown_operation(const aInput: string; aExpected: string);

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

   [Ignore]
   [TestCase('reject problem missing an operand', 'What is 1 plus?,syntax error')]
   procedure problem_missing_an_operand(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('reject problem with no operands or operators', 'What is?,syntax error')]
   procedure problem_with_no_operands_or_operators(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('reject two operations in a row', 'What is 1 plus plus 2?,syntax error')]
   procedure reject_two_operations_in_a_row(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('reject two numbers in a row', 'What is 1 plus 2 1?,syntax error')]
   procedure reject_two_numbers_in_a_row(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('reject postfix notation', 'What is 1 2 plus?,syntax error')]
   procedure reject_postfix_notation(const aInput: string; aExpected: string);

   [Ignore]
   [TestCase('reject prefix notation', 'What is plus 1 2?,syntax error')]
   procedure reject_prefix_notation(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.problem_missing_an_operand(const aInput: string;
  aExpected: string);
var MyProc: TTestLocalMethod;
begin
  MyProc := procedure
            begin
              TWordy.Answer(aInput);
            end;

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

procedure WordyTests.just_a_number(const aInput: string;
  const 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.problem_with_no_operands_or_operators(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;

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

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

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

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

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

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

procedure WordyTests.reject_two_operations_in_a_row(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.Classes, System.SysUtils;

type
  Operations = (Plus, Minus, Multiplied, Divided);

  EInvalidProblem = class(Exception);

  TWordy = class
    private
      class procedure FindOperand;
      class procedure FindOperation;
      class procedure Parse;
      class procedure PerformOperation;
      class procedure PrepareParse;
      class procedure RaiseError(ErrorMessage: String);

      class var FAnswer: Integer;
      class var FItems: TStringList;
      class var FOperand: Integer;
      class var FOperation: Operations;
      class var FProblem: string;

    public
      class function Answer(aInput: String): Integer;

  end;

implementation

var
  OperationWords: TStringList;

{ TWordy }

class function TWordy.Answer(aInput: String): Integer;
begin
  // If the problem string is empty,
  if Length(aInput) = 0 then
    //; signal an error
    RaiseError('syntax error');

  // Trim off any extra spaces
  FProblem := Trim(aInput);

  // Prepare the variables needed to parse the string
  PrepareParse;

  // Parse the string and calculate the answer
  Parse;

  // Return the answer
  Result := FAnswer;
end;

class procedure TWordy.PrepareParse;
const
  Header = 'What is ';
  Trailer = '?';
var
  Index: Integer;
begin
  // A valid problem starts with "What "
  if Copy(FProblem, 1, 5) <> 'What '  then
    // This problem doesn't, so signal an error
    RaiseError('unknown operation');;

  // See if the problem starts with "What is "
  Index := Pos(Header, FProblem);

  // If Index equals zero, the string is missing the header,
  if Index = 0 then
    // so signal a syntax error
    RaiseError('syntax error');;

  // Remove the problem header
  Delete(FProblem, Index, Length(Header));

  // Look for the end of the problem string, which should be a question mark
  Index := Pos(Trailer, FProblem);

  // Does the string end with the question mark?
  if Index = 0 then
    // No, signal a syntax error
    RaiseError('syntax error');

  // Remobe the question mark
  Delete(FProblem, Index, Length(Trailer));

  // Create the string list to contain the valid operations
  OperationWords := TStringList.Create;

  // Populate the valid operation string
  OperationWords.AddStrings(['plus', 'minus', 'multiplied', 'divided']);
end;

class procedure TWordy.RaiseError(ErrorMessage: String);
begin
  // Throw an exception, using the error message passed here
  raise EInvalidProblem.Create(ErrorMessage);
end;

class procedure TWordy.FindOperand;
begin
  // If the problem contains no more words, or if the current words is a valid
  // operator, raise a syntax error
  if (FItems.Count = 0) or (OperationWords.IndexOf(FItems[0]) <> -1) then
    RaiseError('syntax error');

  // Try converting the current word to an integer
  try
    FOperand := StrToInt(FItems[0]);

    // The current word was an integer, so delete the current word
    FItems.Delete(0);

  except
    // We couldn't convert the word to an integer, so raise a syntax error
    RaiseError('syntax error');

  end;
end;

class procedure TWordy.FindOperation;
var
  Index: Integer;
begin
  // See if the current word is a valid operation
  Index := OperationWords.IndexOf(FItems[0]);

  if Index >= 0 then
    begin
      // This is a valid operation, now get which operation
      FOperation := Operations(Index);

      // The 'multiplied' and 'divided' operations are followed by 'by',
      // so we need to delete that word
      if Operations(Index) in [Multiplied .. Divided] then
        if FItems[1] = 'by' then
          FItems.Delete(1);
    end
  else
    // Is the current erroneous word a number?
    if TryStrToInt(FItems[0], Index) then
      // Yes, so raise a syntax errir
      RaiseError('syntax error')
    else
      // No, so raise an unknown operation error
      RaiseError('unknown operation');

  // Delete the current word
  FItems.Delete(0);
end;

class procedure TWordy.Parse;
begin
  // Initially, the answer is always zero
  FAnswer := 0;

  // Create the list of words in problem
  FItems := TStringList.Create;

  // Clear the list
  FItems.Clear;

  // The list is delimited by spaces
  FItems.Delimiter := ' ';

  // Set the space as the only delimiter
  FItems.StrictDelimiter := True;

  // Now, break the problem string into words
  FItems.DelimitedText := FProblem;

  // There is an implict "plus" at the beginning to add the first operand to
  // the answer, so let's insert it into the list.
  FItems.Insert(0, OperationWords[Ord(Plus)]);

  // While there are words left to process
  while FItems.Count > 0 do
    begin
      // We find the next operation
      FindOperation;

      // and it's operand
      FindOperand;

      // and perform the operation designated by the operation
      PerformOperation;
    end;

  // No more words to process, so clean up the word list
  FItems.Destroy;
end;

class procedure TWordy.PerformOperation;
begin
  // What operation is requested?
  case FOperation of
    // Addition
    Plus:
      // Add the operand to the current answer
      Inc(FAnswer, FOperand);

    // Subtraction
    Minus:
      // Subtract the operand from the current answer
      Dec(FAnswer, FOperand);

    // Mulitplication
    Multiplied:
      // Multiply the current answer by the operand
      FAnswer := FAnswer * FOperand;

    // Divison
    Divided:
      // (Integer) Divide the current answer by the operand
      FAnswer := FAnswer div FOperand;
  end;
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?