Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created April 28, 2020 02:02
Show Gist options
  • Save jackcallister/717578cf38cd11540a15f6c525fab6b9 to your computer and use it in GitHub Desktop.
Save jackcallister/717578cf38cd11540a15f6c525fab6b9 to your computer and use it in GitHub Desktop.
; (deftest large-addition
; (is (= (wordy/evaluate "What is 123 plus 45678?") 45801)))
; (deftest addition-and-multiplication
; (is (= (wordy/evaluate "What is -3 plus 7 multiplied by -2?") -8)))
(ns wordy (:use clojure.string))
(defn remove-question-mark [query]
(clojure.string/join "" (drop-last query)))
(defn remove-extra-chars [query]
(clojure.string/replace query #"by|\?" ""))
(defn split-to-pairs [query]
(partition-all 2 (nthrest (filter #(not (empty? %)) (clojure.string/split query #" ")) 2)))
(def op-map {
"plus" +
"minus" -
"multiplied" *
"divided" /
})
(defn evaluate [query]
(reduce (fn [acc [n op]]
(if (boolean op)
(partial (op-map op) (acc (Integer/parseInt n)))
(acc (Integer/parseInt n)))) (partial + 0) (-> query remove-extra-chars split-to-pairs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment