Bläddra i källkod

wordy: add to track (#192)

Stuart Kent 9 år sedan
förälder
incheckning
470f4b2b2e

+ 6
- 0
config.json Visa fil

55
     "binary-search-tree",
55
     "binary-search-tree",
56
     "binary-search",
56
     "binary-search",
57
     "all-your-base",
57
     "all-your-base",
58
+    "wordy",
58
     "palindrome-products"
59
     "palindrome-products"
59
   ],
60
   ],
60
   "exercises": [
61
   "exercises": [
314
       "topics": []
315
       "topics": []
315
     },
316
     },
316
     {
317
     {
318
+      "slug": "wordy",
319
+      "difficulty": 1,
320
+      "topics": []
321
+    },
322
+    {
317
       "slug": "palindrome-products",
323
       "slug": "palindrome-products",
318
       "difficulty": 1,
324
       "difficulty": 1,
319
       "topics": []
325
       "topics": []

+ 1
- 0
exercises/settings.gradle Visa fil

49
 include 'triangle'
49
 include 'triangle'
50
 include 'trinary'
50
 include 'trinary'
51
 include 'word-count'
51
 include 'word-count'
52
+include 'wordy'

+ 17
- 0
exercises/wordy/build.gradle Visa fil

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.12"
11
+}
12
+test {
13
+  testLogging {
14
+    exceptionFormat = 'full'
15
+    events = ["passed", "failed", "skipped"]
16
+  }
17
+}

+ 57
- 0
exercises/wordy/src/example/java/WordProblemSolver.java Visa fil

1
+import java.util.regex.Matcher;
2
+import java.util.regex.Pattern;
3
+
4
+final class WordProblemSolver {
5
+
6
+    private static final String INTEGER_REGEX_STRING = "(-?\\d+)";
7
+
8
+    private static final String BINARY_OPERATION_REGEX_STRING = "(plus|minus|multiplied by|divided by)";
9
+
10
+    private static final String VALID_QUESTION_REGEX_STRING =
11
+            "^What is " + INTEGER_REGEX_STRING +
12
+            "( " + BINARY_OPERATION_REGEX_STRING + " " + INTEGER_REGEX_STRING + ")+" +
13
+            "\\?$";
14
+
15
+    int solve(final String wordProblem) {
16
+        if (!wordProblem.matches(VALID_QUESTION_REGEX_STRING)) {
17
+            throw new IllegalArgumentException("I'm sorry, I don't understand the question!");
18
+        }
19
+
20
+        final Matcher initialValueMatcher = Pattern.compile(INTEGER_REGEX_STRING).matcher(wordProblem);
21
+
22
+        initialValueMatcher.find();
23
+        int result = Integer.parseInt(initialValueMatcher.group());
24
+
25
+        final Matcher operationAndValueMatcher
26
+                = Pattern.compile(BINARY_OPERATION_REGEX_STRING + " " + INTEGER_REGEX_STRING).matcher(wordProblem);
27
+
28
+        while (operationAndValueMatcher.find()) {
29
+            result = applyBinaryOperation(
30
+                result,
31
+                operationAndValueMatcher.group(1),
32
+                Integer.parseInt(operationAndValueMatcher.group(2)));
33
+        }
34
+
35
+        return result;
36
+    }
37
+
38
+    private int applyBinaryOperation(
39
+            final int firstValue,
40
+            final String operationDescription,
41
+            final int secondValue) {
42
+
43
+        switch (operationDescription) {
44
+            case "plus":
45
+                return firstValue + secondValue;
46
+            case "minus":
47
+                return firstValue - secondValue;
48
+            case "multiplied by":
49
+                return firstValue * secondValue;
50
+            case "divided by":
51
+                return firstValue / secondValue;
52
+            default:
53
+                throw new IllegalArgumentException("This branch should never be executed.");
54
+        }
55
+    }
56
+
57
+}

+ 5
- 0
exercises/wordy/src/main/java/WordProblemSolver.java Visa fil

1
+final class WordProblemSolver {
2
+
3
+
4
+
5
+}

+ 119
- 0
exercises/wordy/src/test/java/WordProblemSolverTest.java Visa fil

1
+import org.junit.Ignore;
2
+import org.junit.Rule;
3
+import org.junit.Test;
4
+import org.junit.rules.ExpectedException;
5
+
6
+import static org.junit.Assert.assertEquals;
7
+
8
+public final class WordProblemSolverTest {
9
+
10
+    /*
11
+     * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
12
+     * ExpectedExceptions in particular.
13
+     */
14
+    @Rule
15
+    public ExpectedException expectedException = ExpectedException.none();
16
+
17
+    @Test
18
+    public void testSingleAddition1() {
19
+        assertEquals(2, new WordProblemSolver().solve("What is 1 plus 1?"));
20
+    }
21
+
22
+    @Ignore
23
+    @Test
24
+    public void testSingleAddition2() {
25
+        assertEquals(55, new WordProblemSolver().solve("What is 53 plus 2?"));
26
+    }
27
+
28
+    @Ignore
29
+    @Test
30
+    public void testSingleAdditionWithNegativeNumbers() {
31
+        assertEquals(-11, new WordProblemSolver().solve("What is -1 plus -10?"));
32
+    }
33
+
34
+    @Ignore
35
+    @Test
36
+    public void testSingleAdditionOfLargeNumbers() {
37
+        assertEquals(45801, new WordProblemSolver().solve("What is 123 plus 45678?"));
38
+    }
39
+
40
+    @Ignore
41
+    @Test
42
+    public void testSingleSubtraction() {
43
+        assertEquals(16, new WordProblemSolver().solve("What is 4 minus -12?"));
44
+    }
45
+
46
+    @Ignore
47
+    @Test
48
+    public void testSingleMultiplication() {
49
+        assertEquals(-75, new WordProblemSolver().solve("What is -3 multiplied by 25?"));
50
+    }
51
+
52
+    @Ignore
53
+    @Test
54
+    public void testSingleDivision() {
55
+        assertEquals(-11, new WordProblemSolver().solve("What is 33 divided by -3?"));
56
+    }
57
+
58
+    @Ignore
59
+    @Test
60
+    public void testMultipleAdditions() {
61
+        assertEquals(3, new WordProblemSolver().solve("What is 1 plus 1 plus 1?"));
62
+    }
63
+
64
+    @Ignore
65
+    @Test
66
+    public void testAdditionThenSubtraction() {
67
+        assertEquals(8, new WordProblemSolver().solve("What is 1 plus 5 minus -2?"));
68
+    }
69
+
70
+    @Ignore
71
+    @Test
72
+    public void testMultipleSubtractions() {
73
+        assertEquals(3, new WordProblemSolver().solve("What is 20 minus 4 minus 13?"));
74
+    }
75
+
76
+    @Ignore
77
+    @Test
78
+    public void testSubtractionThenAddition() {
79
+        assertEquals(14, new WordProblemSolver().solve("What is 17 minus 6 plus 3?"));
80
+    }
81
+
82
+    @Ignore
83
+    @Test
84
+    public void testMultipleMultiplications() {
85
+        assertEquals(-12, new WordProblemSolver().solve("What is 2 multiplied by -2 multiplied by 3?"));
86
+    }
87
+
88
+    @Ignore
89
+    @Test
90
+    public void testAdditionThenMultiplication() {
91
+        assertEquals(-8, new WordProblemSolver().solve("What is -3 plus 7 multiplied by -2?"));
92
+    }
93
+
94
+    @Ignore
95
+    @Test
96
+    public void testMultipleDivisions() {
97
+        assertEquals(2, new WordProblemSolver().solve("What is -12 divided by 2 divided by -3?"));
98
+    }
99
+
100
+    @Ignore
101
+    @Test
102
+    public void testUnknownOperation() {
103
+        expectedException.expect(IllegalArgumentException.class);
104
+        expectedException.expectMessage("I'm sorry, I don't understand the question!");
105
+
106
+        new WordProblemSolver().solve("What is 52 cubed?");
107
+    }
108
+
109
+    @Ignore
110
+    @Test
111
+    public void testInvalidQuestionFormat() {
112
+        expectedException.expect(IllegalArgumentException.class);
113
+        expectedException.expectMessage("I'm sorry, I don't understand the question!");
114
+
115
+        // See https://en.wikipedia.org/wiki/President_of_the_United_States if you really need to know!
116
+        new WordProblemSolver().solve("Who is the President of the United States?");
117
+    }
118
+
119
+}