Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wordy: update to latest #745

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 17 additions & 8 deletions exercises/practice/wordy/.meta/example.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@

(defn- parse-op [op-str]
(or (ops op-str)
(throw (IllegalArgumentException. (str "unknown operator " op-str)))))
(if (every? Character/isDigit op-str)
(throw (IllegalArgumentException. "syntax error"))
(throw (IllegalArgumentException. "unknown operation")))))

(defn- parse-operand [operand-str]
(try
(Integer/parseInt operand-str)
(catch java.lang.NumberFormatException _ (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"))))
(if-let [[_ exprs] (re-matches #"What is(.*)\?" expr)]
(if (empty? exprs)
(throw (IllegalArgumentException. "syntax error"))
(if-let [[token & tokens] (re-seq tokens-pattern exprs)]
(reduce (fn [acc [op x]]
((parse-op op) acc (parse-operand x)))
(parse-operand token) (partition-all 2 tokens))
(throw (IllegalArgumentException. "unknown operation"))))
(throw (IllegalArgumentException. "syntax error"))))
11 changes: 11 additions & 0 deletions exercises/practice/wordy/.meta/generator.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns wordy-generator)

(defn normalize-error [test-case error]
(if (= "Non math question" (:description test-case))
"^syntax error$"
(str "^" error "$")))

(defn transform-test-case [test-case]
(if-let [error (get-in test-case [:expected :error])]
(assoc-in test-case [:expected :error] (normalize-error test-case error))
test-case))
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
"Evaluates 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 #"^syntax error$" (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?")))))