Skip to content

Commit cfec606

Browse files
wordy: update to latest
[no important files changed]
1 parent c0b457b commit cfec606

File tree

5 files changed

+129
-65
lines changed

5 files changed

+129
-65
lines changed

exercises/practice/wordy/.meta/config.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"AndreaCrotti",
77
"haus",
88
"sjwarner-bp",
9-
"yurrriq"
9+
"yurrriq",
10+
"erikschierboom"
1011
],
1112
"files": {
1213
"solution": [

exercises/practice/wordy/.meta/example.clj

+17-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@
1111

1212
(defn- parse-op [op-str]
1313
(or (ops op-str)
14-
(throw (IllegalArgumentException. (str "unknown operator " op-str)))))
14+
(if (every? Character/isDigit op-str)
15+
(throw (IllegalArgumentException. "syntax error"))
16+
(throw (IllegalArgumentException. "unknown operation")))))
17+
18+
(defn- parse-operand [operand-str]
19+
(try
20+
(Integer/parseInt operand-str)
21+
(catch java.lang.NumberFormatException _ (throw (IllegalArgumentException. "syntax error")))))
1522

1623
(defn evaluate [expr]
17-
(if-let [[_ exprs] (re-matches #"What is (.+)\?" expr)]
18-
(if-let [[token & tokens] (re-seq tokens-pattern exprs)]
19-
(reduce (fn [acc [op x]]
20-
((parse-op op) acc (Integer/parseInt x)))
21-
(Integer/parseInt token) (partition-all 2 tokens))
22-
(throw (IllegalArgumentException. "no arithmetic expression found")))
23-
(throw (IllegalArgumentException. "cannot recognize question"))))
24+
(if-let [[_ exprs] (re-matches #"What is(.*)\?" expr)]
25+
(if (empty? exprs)
26+
(throw (IllegalArgumentException. "syntax error"))
27+
(if-let [[token & tokens] (re-seq tokens-pattern exprs)]
28+
(reduce (fn [acc [op x]]
29+
((parse-op op) acc (parse-operand x)))
30+
(parse-operand token) (partition-all 2 tokens))
31+
(throw (IllegalArgumentException. "unknown operation"))))
32+
(throw (IllegalArgumentException. "syntax error"))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns wordy-test
2+
(:require [clojure.test :refer [deftest testing is]]
3+
wordy))
4+
{{#test_cases.answer}}
5+
(deftest evaluate_test_{{idx}}
6+
(testing {{description}}
7+
{{~#if error}}
8+
(is (thrown-with-msg? IllegalArgumentException #{{error}} (wordy/evaluate {{input.question}})))))
9+
{{else}}
10+
(is (= {{expected}} (wordy/evaluate {{input.question}})))))
11+
{{/if~}}
12+
{{/test_cases.answer~}}
+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns wordy)
22

3-
(defn evaluate [] ;; <- arglist goes here
4-
;; your code goes here
5-
)
3+
(defn evaluate
4+
"Evaluate a simple math problem"
5+
[question]
6+
;; function body
7+
)
+93-53
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,96 @@
11
(ns wordy-test
2-
(:require [clojure.test :refer [deftest is]]
2+
(:require [clojure.test :refer [deftest testing is]]
33
wordy))
44

5-
(deftest addition
6-
(is (= (wordy/evaluate "What is 1 plus 1?") 2)))
7-
8-
(deftest more-addition
9-
(is (= (wordy/evaluate "What is 53 plus 2?") 55)))
10-
11-
(deftest addition-with-negative-numbers
12-
(is (= (wordy/evaluate "What is -1 plus -10?") -11)))
13-
14-
(deftest large-addition
15-
(is (= (wordy/evaluate "What is 123 plus 45678?") 45801)))
16-
17-
(deftest subtraction
18-
(is (= (wordy/evaluate "What is 4 minus -12?") 16)))
19-
20-
(deftest multiplication
21-
(is (= (wordy/evaluate "What is -3 multiplied by 25?") -75)))
22-
23-
(deftest division
24-
(is (= (wordy/evaluate "What is 33 divided by -3?") -11)))
25-
26-
(deftest multiple-additions
27-
(is (= (wordy/evaluate "What is 1 plus 1 plus 1?") 3)))
28-
29-
(deftest addition-and-subtraction
30-
(is (= (wordy/evaluate "What is 1 plus 5 minus -2?") 8)))
31-
32-
(deftest multiple-subtraction
33-
(is (= (wordy/evaluate "What is 20 minus 4 minus 13?") 3)))
34-
35-
(deftest subtraction-then-addition
36-
(is (= (wordy/evaluate "What is 17 minus 6 plus 3?") 14)))
37-
38-
(deftest multiple-multiplication
39-
(is (= (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?") -12)))
40-
41-
(deftest addition-and-multiplication
42-
(is (= (wordy/evaluate "What is -3 plus 7 multiplied by -2?") -8)))
43-
44-
(deftest multiple-division
45-
(is (= (wordy/evaluate "What is -12 divided by 2 divided by -3?") 2)))
46-
47-
(deftest unknown-operation
48-
(is (thrown?
49-
IllegalArgumentException
50-
(wordy/evaluate "What is 52 cubed?"))))
51-
52-
(deftest Non-math-question
53-
(is (thrown?
54-
IllegalArgumentException
55-
(wordy/evaluate "Who is the President of the United States?"))))
56-
5+
(deftest evaluate_test_1
6+
(testing "just a number"
7+
(is (= 5 (wordy/evaluate "What is 5?")))))
8+
9+
(deftest evaluate_test_2
10+
(testing "addition"
11+
(is (= 2 (wordy/evaluate "What is 1 plus 1?")))))
12+
13+
(deftest evaluate_test_3
14+
(testing "more addition"
15+
(is (= 55 (wordy/evaluate "What is 53 plus 2?")))))
16+
17+
(deftest evaluate_test_4
18+
(testing "addition with negative numbers"
19+
(is (= -11 (wordy/evaluate "What is -1 plus -10?")))))
20+
21+
(deftest evaluate_test_5
22+
(testing "large addition"
23+
(is (= 45801 (wordy/evaluate "What is 123 plus 45678?")))))
24+
25+
(deftest evaluate_test_6
26+
(testing "subtraction"
27+
(is (= 16 (wordy/evaluate "What is 4 minus -12?")))))
28+
29+
(deftest evaluate_test_7
30+
(testing "multiplication"
31+
(is (= -75 (wordy/evaluate "What is -3 multiplied by 25?")))))
32+
33+
(deftest evaluate_test_8
34+
(testing "division"
35+
(is (= -11 (wordy/evaluate "What is 33 divided by -3?")))))
36+
37+
(deftest evaluate_test_9
38+
(testing "multiple additions"
39+
(is (= 3 (wordy/evaluate "What is 1 plus 1 plus 1?")))))
40+
41+
(deftest evaluate_test_10
42+
(testing "addition and subtraction"
43+
(is (= 8 (wordy/evaluate "What is 1 plus 5 minus -2?")))))
44+
45+
(deftest evaluate_test_11
46+
(testing "multiple subtraction"
47+
(is (= 3 (wordy/evaluate "What is 20 minus 4 minus 13?")))))
48+
49+
(deftest evaluate_test_12
50+
(testing "subtraction then addition"
51+
(is (= 14 (wordy/evaluate "What is 17 minus 6 plus 3?")))))
52+
53+
(deftest evaluate_test_13
54+
(testing "multiple multiplication"
55+
(is (= -12 (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?")))))
56+
57+
(deftest evaluate_test_14
58+
(testing "addition and multiplication"
59+
(is (= -8 (wordy/evaluate "What is -3 plus 7 multiplied by -2?")))))
60+
61+
(deftest evaluate_test_15
62+
(testing "multiple division"
63+
(is (= 2 (wordy/evaluate "What is -12 divided by 2 divided by -3?")))))
64+
65+
(deftest evaluate_test_16
66+
(testing "unknown operation"
67+
(is (thrown-with-msg? IllegalArgumentException #"unknown operation" (wordy/evaluate "What is 52 cubed?")))))
68+
69+
(deftest evaluate_test_17
70+
(testing "Non math question"
71+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "Who is the President of the United States?")))))
72+
73+
(deftest evaluate_test_18
74+
(testing "reject problem missing an operand"
75+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus?")))))
76+
77+
(deftest evaluate_test_19
78+
(testing "reject problem with no operands or operators"
79+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is?")))))
80+
81+
(deftest evaluate_test_20
82+
(testing "reject two operations in a row"
83+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus plus 2?")))))
84+
85+
(deftest evaluate_test_21
86+
(testing "reject two numbers in a row"
87+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus 2 1?")))))
88+
89+
(deftest evaluate_test_22
90+
(testing "reject postfix notation"
91+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 2 plus?")))))
92+
93+
(deftest evaluate_test_23
94+
(testing "reject prefix notation"
95+
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is plus 1 2?")))))
96+

0 commit comments

Comments
 (0)