 # artemkorsakov's solution

## to Wordy in the Java Track

Published at Apr 13 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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.

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

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

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

### WordProblemSolverTest.java

``````import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

public class WordProblemSolverTest {

@Rule
public ExpectedException expectedException = ExpectedException.none();

WordProblemSolver solver;

@Before
public void setup() {
solver = new WordProblemSolver();
}

@Test
assertEquals(2, solver.solve("What is 1 plus 1?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(55, solver.solve("What is 53 plus 2?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(-11, solver.solve("What is -1 plus -10?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(45801, solver.solve("What is 123 plus 45678?"));
}

@Ignore("Remove to run test")
@Test
public void testSingleSubtraction() {
assertEquals(16, solver.solve("What is 4 minus -12?"));
}

@Ignore("Remove to run test")
@Test
public void testSingleMultiplication() {
assertEquals(-75, solver.solve("What is -3 multiplied by 25?"));
}

@Ignore("Remove to run test")
@Test
public void testSingleDivision() {
assertEquals(-11, solver.solve("What is 33 divided by -3?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(3, solver.solve("What is 1 plus 1 plus 1?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(8, solver.solve("What is 1 plus 5 minus -2?"));
}

@Ignore("Remove to run test")
@Test
public void testMultipleSubtractions() {
assertEquals(3, solver.solve("What is 20 minus 4 minus 13?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(14, solver.solve("What is 17 minus 6 plus 3?"));
}

@Ignore("Remove to run test")
@Test
public void testMultipleMultiplications() {
assertEquals(-12, solver.solve("What is 2 multiplied by -2 multiplied by 3?"));
}

@Ignore("Remove to run test")
@Test
assertEquals(-8, solver.solve("What is -3 plus 7 multiplied by -2?"));
}

@Ignore("Remove to run test")
@Test
public void testMultipleDivisions() {
assertEquals(2, solver.solve("What is -12 divided by 2 divided by -3?"));
}

@Ignore("Remove to run test")
@Test
public void testUnknownOperation() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("I'm sorry, I don't understand the question!");

solver.solve("What is 52 cubed?");
}

@Ignore("Remove to run test")
@Test
public void testInvalidQuestionFormat() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("I'm sorry, I don't understand the question!");

// See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
solver.solve("Who is the President of the United States?");
}

}``````
``````import java.util.regex.Matcher;
import java.util.regex.Pattern;

class WordProblemSolver {
private static Pattern patternNumber = Pattern.compile("-?\\d+");
private static Pattern patternInitialNumber = Pattern.compile("What is " + patternNumber);
private static Pattern patternAddition = Pattern.compile(" plus " + patternNumber);
private static Pattern patternSubtraction = Pattern.compile(" minus " + patternNumber);
private static Pattern patternMultiplication = Pattern.compile(" multiplied by " + patternNumber);
private static Pattern patternDivision = Pattern.compile(" divided by " + patternNumber);
private static Pattern patternPower = Pattern.compile(" raised to the \\d+[a-z]{2} power");
private static Pattern patternMembers = Pattern.compile("(" + patternAddition + "|" + patternSubtraction + "|"
+ patternMultiplication + "|" + patternDivision + "|" + patternPower + ")");
private static Pattern patternAll = Pattern.compile("^" + patternInitialNumber + patternMembers + "+\\?\$");

int solve(String input) {
if (!patternAll.matcher(input).matches()) {
throw new IllegalArgumentException("I'm sorry, I don't understand the question!");
}

int result = getInitial(input);
Matcher matcher = patternMembers.matcher(input);
while (matcher.find()) {
result = solve(input.substring(matcher.start(), matcher.end()), result);
}
return result;
}

private int getInitial(String input) {
Matcher matcher = patternInitialNumber.matcher(input);
matcher.find();
return getNumber(input.substring(matcher.start(), matcher.end()));
}

private int solve(String input, int result) {
if (matcher.find()) {
return result + getNumber(input.substring(matcher.start(), matcher.end()));
}
matcher = patternSubtraction.matcher(input);
if (matcher.find()) {
return result - getNumber(input.substring(matcher.start(), matcher.end()));
}
matcher = patternMultiplication.matcher(input);
if (matcher.find()) {
return result * getNumber(input.substring(matcher.start(), matcher.end()));
}
matcher = patternDivision.matcher(input);
if (matcher.find()) {
return result / getNumber(input.substring(matcher.start(), matcher.end()));
}
matcher = patternPower.matcher(input);
if (matcher.find()) {
return (int) Math.pow(result, getNumber(input.substring(matcher.start(), matcher.end())));
}
return result;
}

private int getNumber(String input) {
Matcher matcher = patternNumber.matcher(input);
matcher.find();
return Integer.parseInt(input.substring(matcher.start(), matcher.end()));
}
}``````