# Sirius708's solution

## to Wordy in the Java 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.

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

# Running the tests

You can run all the tests for an exercise by entering

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

## 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.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordProblemSolver {
private static final Map<String, String> WORD_TO_OPERATION = new HashMap<>();
private static final Pattern NUMBER_PATTERN = Pattern.compile("([-]?\\d+)");
private static final Pattern OPERATION_PATTERN;

static {
WORD_TO_OPERATION.put("plus", "+");
WORD_TO_OPERATION.put("minus", "-");
WORD_TO_OPERATION.put("multiplied", "*");
WORD_TO_OPERATION.put("divided", "/");
WORD_TO_OPERATION.put("raised", "^");

OPERATION_PATTERN = Pattern.compile("(" + String.join("|", WORD_TO_OPERATION.keySet()) + ")", Pattern.CASE_INSENSITIVE);
}

public int solve(String problem) {
List<String> numberGroups = new ArrayList<>();
Matcher numberMatcher = NUMBER_PATTERN.matcher(problem);
while (numberMatcher.find()) {
}

List<String> operationGroups = new ArrayList<>();
Matcher operationMatcher = OPERATION_PATTERN.matcher(problem);
while (operationMatcher.find()) {
}

if (operationGroups.size() == 0 || numberGroups.size() == 0) {
throw new IllegalArgumentException("I'm sorry, I don't understand the question!");
}

Stack<Integer> numbers = new Stack<>();
Queue<String> operations = new ArrayDeque<>();

for (int i = numberGroups.size() - 1; i >= 0; i--) {
numbers.push(Integer.parseInt(numberGroups.get(i)));
}
for (String word : operationGroups) {
operations.offer(WORD_TO_OPERATION.get(word));
}

for (String op : operations) {
int n1 = numbers.pop();
int n2 = numbers.pop();

switch (op) {
case "+": numbers.push(n1 + n2); break;
case "-": numbers.push(n1 - n2); break;
case "*": numbers.push(n1 * n2); break;
case "/": numbers.push(n1 / n2); break;
case "^": numbers.push((int) Math.pow(n1, n2));
}
}

if (numbers.size() == 1) return numbers.pop();

throw new IllegalArgumentException("I'm sorry, I don't understand the question!");
}
}``````

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