Sha256: 488475d865935a21e8680ecf2d5b77ff4ab547119b64b9fa52db19c5b67c6a6c

Contents?: true

Size: 1.95 KB

Versions: 358

Compression:

Stored size: 1.95 KB

Contents

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

final class WordProblemSolver {

    private static final String INTEGER_REGEX_STRING = "(-?\\d+)";

    private static final String BINARY_OPERATION_REGEX_STRING = "(plus|minus|multiplied by|divided by)";

    private static final String VALID_QUESTION_REGEX_STRING =
            "^What is " + INTEGER_REGEX_STRING +
            "( " + BINARY_OPERATION_REGEX_STRING + " " + INTEGER_REGEX_STRING + ")+" +
            "\\?$";

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

        final Matcher initialValueMatcher = Pattern.compile(INTEGER_REGEX_STRING).matcher(wordProblem);

        initialValueMatcher.find();
        int result = Integer.parseInt(initialValueMatcher.group());

        final Matcher operationAndValueMatcher
                = Pattern.compile(BINARY_OPERATION_REGEX_STRING + " " + INTEGER_REGEX_STRING).matcher(wordProblem);

        while (operationAndValueMatcher.find()) {
            result = applyBinaryOperation(
                result,
                operationAndValueMatcher.group(1),
                Integer.parseInt(operationAndValueMatcher.group(2)));
        }

        return result;
    }

    private int applyBinaryOperation(
            final int firstValue,
            final String operationDescription,
            final int secondValue) {

        switch (operationDescription) {
            case "plus":
                return firstValue + secondValue;
            case "minus":
                return firstValue - secondValue;
            case "multiplied by":
                return firstValue * secondValue;
            case "divided by":
                return firstValue / secondValue;
            default:
                throw new IllegalArgumentException("This branch should never be executed.");
        }
    }

}

Version data entries

358 entries across 358 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.179 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.178 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.177 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.176 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.175 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.174 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.173 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.172 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.171 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.170 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.169 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.167 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.166 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.165 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.164 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.163 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.162 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.161 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java
trackler-2.2.1.160 tracks/java/exercises/wordy/.meta/src/reference/java/WordProblemSolver.java