Skip to content

Commit

Permalink
wordy: update to latest
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
ErikSchierboom committed Jan 15, 2025
1 parent c0b457b commit 04a5244
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 60 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/wordy/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"AndreaCrotti",
"haus",
"sjwarner-bp",
"yurrriq"
"yurrriq",
"erikschierboom"
],
"files": {
"solution": [
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/wordy/.meta/example.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

(defn- parse-op [op-str]
(or (ops op-str)
(throw (IllegalArgumentException. (str "unknown operator " op-str)))))
(throw (IllegalArgumentException. "syntax error"))))

(defn evaluate [expr]
(if-let [[_ exprs] (re-matches #"What is (.+)\?" expr)]
(if-let [[token & tokens] (re-seq tokens-pattern exprs)]
(reduce (fn [acc [op x]]
((parse-op op) acc (Integer/parseInt x)))
(Integer/parseInt token) (partition-all 2 tokens))
(throw (IllegalArgumentException. "no arithmetic expression found")))
(throw (IllegalArgumentException. "cannot recognize question"))))
(throw (IllegalArgumentException. "syntax error")))
(throw (IllegalArgumentException. "syntax error"))))
12 changes: 12 additions & 0 deletions exercises/practice/wordy/.meta/generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns wordy-test
(:require [clojure.test :refer [deftest testing is]]
wordy))
{{#test_cases.answer}}
(deftest evaluate_test_{{idx}}
(testing {{description}}
{{~#if error}}
(is (thrown-with-msg? IllegalArgumentException #{{error}} (wordy/evaluate {{input.question}})))))
{{else}}
(is (= {{expected}} (wordy/evaluate {{input.question}})))))
{{/if~}}
{{/test_cases.answer~}}
8 changes: 5 additions & 3 deletions exercises/practice/wordy/src/wordy.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns wordy)

(defn evaluate [] ;; <- arglist goes here
;; your code goes here
)
(defn evaluate
"Evaluate a simple math problem"
[question]
;; function body
)
146 changes: 93 additions & 53 deletions exercises/practice/wordy/test/wordy_test.clj
Original file line number Diff line number Diff line change
@@ -1,56 +1,96 @@
(ns wordy-test
(:require [clojure.test :refer [deftest is]]
(:require [clojure.test :refer [deftest testing is]]
wordy))

(deftest addition
(is (= (wordy/evaluate "What is 1 plus 1?") 2)))

(deftest more-addition
(is (= (wordy/evaluate "What is 53 plus 2?") 55)))

(deftest addition-with-negative-numbers
(is (= (wordy/evaluate "What is -1 plus -10?") -11)))

(deftest large-addition
(is (= (wordy/evaluate "What is 123 plus 45678?") 45801)))

(deftest subtraction
(is (= (wordy/evaluate "What is 4 minus -12?") 16)))

(deftest multiplication
(is (= (wordy/evaluate "What is -3 multiplied by 25?") -75)))

(deftest division
(is (= (wordy/evaluate "What is 33 divided by -3?") -11)))

(deftest multiple-additions
(is (= (wordy/evaluate "What is 1 plus 1 plus 1?") 3)))

(deftest addition-and-subtraction
(is (= (wordy/evaluate "What is 1 plus 5 minus -2?") 8)))

(deftest multiple-subtraction
(is (= (wordy/evaluate "What is 20 minus 4 minus 13?") 3)))

(deftest subtraction-then-addition
(is (= (wordy/evaluate "What is 17 minus 6 plus 3?") 14)))

(deftest multiple-multiplication
(is (= (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?") -12)))

(deftest addition-and-multiplication
(is (= (wordy/evaluate "What is -3 plus 7 multiplied by -2?") -8)))

(deftest multiple-division
(is (= (wordy/evaluate "What is -12 divided by 2 divided by -3?") 2)))

(deftest unknown-operation
(is (thrown?
IllegalArgumentException
(wordy/evaluate "What is 52 cubed?"))))

(deftest Non-math-question
(is (thrown?
IllegalArgumentException
(wordy/evaluate "Who is the President of the United States?"))))

(deftest evaluate_test_1
(testing "just a number"
(is (= 5 (wordy/evaluate "What is 5?")))))

(deftest evaluate_test_2
(testing "addition"
(is (= 2 (wordy/evaluate "What is 1 plus 1?")))))

(deftest evaluate_test_3
(testing "more addition"
(is (= 55 (wordy/evaluate "What is 53 plus 2?")))))

(deftest evaluate_test_4
(testing "addition with negative numbers"
(is (= -11 (wordy/evaluate "What is -1 plus -10?")))))

(deftest evaluate_test_5
(testing "large addition"
(is (= 45801 (wordy/evaluate "What is 123 plus 45678?")))))

(deftest evaluate_test_6
(testing "subtraction"
(is (= 16 (wordy/evaluate "What is 4 minus -12?")))))

(deftest evaluate_test_7
(testing "multiplication"
(is (= -75 (wordy/evaluate "What is -3 multiplied by 25?")))))

(deftest evaluate_test_8
(testing "division"
(is (= -11 (wordy/evaluate "What is 33 divided by -3?")))))

(deftest evaluate_test_9
(testing "multiple additions"
(is (= 3 (wordy/evaluate "What is 1 plus 1 plus 1?")))))

(deftest evaluate_test_10
(testing "addition and subtraction"
(is (= 8 (wordy/evaluate "What is 1 plus 5 minus -2?")))))

(deftest evaluate_test_11
(testing "multiple subtraction"
(is (= 3 (wordy/evaluate "What is 20 minus 4 minus 13?")))))

(deftest evaluate_test_12
(testing "subtraction then addition"
(is (= 14 (wordy/evaluate "What is 17 minus 6 plus 3?")))))

(deftest evaluate_test_13
(testing "multiple multiplication"
(is (= -12 (wordy/evaluate "What is 2 multiplied by -2 multiplied by 3?")))))

(deftest evaluate_test_14
(testing "addition and multiplication"
(is (= -8 (wordy/evaluate "What is -3 plus 7 multiplied by -2?")))))

(deftest evaluate_test_15
(testing "multiple division"
(is (= 2 (wordy/evaluate "What is -12 divided by 2 divided by -3?")))))

(deftest evaluate_test_16
(testing "unknown operation"
(is (thrown-with-msg? IllegalArgumentException #"unknown operation" (wordy/evaluate "What is 52 cubed?")))))

(deftest evaluate_test_17
(testing "Non math question"
(is (thrown-with-msg? IllegalArgumentException #"unknown operation" (wordy/evaluate "Who is the President of the United States?")))))

(deftest evaluate_test_18
(testing "reject problem missing an operand"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus?")))))

(deftest evaluate_test_19
(testing "reject problem with no operands or operators"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is?")))))

(deftest evaluate_test_20
(testing "reject two operations in a row"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus plus 2?")))))

(deftest evaluate_test_21
(testing "reject two numbers in a row"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 plus 2 1?")))))

(deftest evaluate_test_22
(testing "reject postfix notation"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is 1 2 plus?")))))

(deftest evaluate_test_23
(testing "reject prefix notation"
(is (thrown-with-msg? IllegalArgumentException #"syntax error" (wordy/evaluate "What is plus 1 2?")))))

0 comments on commit 04a5244

Please sign in to comment.