This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fff71b7
commit 4a39a21
Showing
8 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Instructions | ||
|
||
Parse and evaluate simple math word problems returning the answer as an integer. | ||
|
||
## Iteration 0 — Numbers | ||
|
||
Problems with no operations simply evaluate to the number given. | ||
|
||
> What is 5? | ||
Evaluates to 5. | ||
|
||
## Iteration 1 — Addition | ||
|
||
Add two numbers together. | ||
|
||
> What is 5 plus 13? | ||
Evaluates to 18. | ||
|
||
Handle large numbers and negative numbers. | ||
|
||
## Iteration 2 — Subtraction, Multiplication and Division | ||
|
||
Now, perform the other three operations. | ||
|
||
> What is 7 minus 5? | ||
2 | ||
|
||
> What is 6 multiplied by 4? | ||
24 | ||
|
||
> What is 25 divided by 5? | ||
5 | ||
|
||
## Iteration 3 — Multiple Operations | ||
|
||
Handle a set of operations, in sequence. | ||
|
||
Since these are verbal word problems, evaluate the expression from left-to-right, _ignoring the typical order of operations._ | ||
|
||
> What is 5 plus 13 plus 6? | ||
24 | ||
|
||
> What is 3 plus 2 multiplied by 3? | ||
15 (i.e. not 9) | ||
|
||
## Iteration 4 — Errors | ||
|
||
The parser should reject: | ||
|
||
- Unsupported operations ("What is 52 cubed?") | ||
- Non-math questions ("Who is the President of the United States") | ||
- Word problems with invalid syntax ("What is 1 plus plus 2?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/wordy.cljs" | ||
], | ||
"test": [ | ||
"test/wordy_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"blurb": "Parse and evaluate simple math word problems returning the answer as an integer.", | ||
"source": "Inspired by one of the generated questions in the Extreme Startup game.", | ||
"source_url": "https://github.com/rchatley/extreme_startup" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(ns wordy | ||
(:require [clojure.string :refer [join]])) | ||
|
||
(def ^:private ops {"plus" + | ||
"minus" - | ||
"multiplied by" * | ||
"divided by" /}) | ||
|
||
(def ^:private tokens-pattern (re-pattern | ||
(str (join "|" (keys ops)) "|-?\\d+|\\S+"))) | ||
|
||
(defn- parse-op [op-str] | ||
(or (ops op-str) | ||
(throw (js/Error. (str "unknown operator " op-str))))) | ||
|
||
(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 (js/parseInt x))) | ||
(js/parseInt token) (partition-all 2 tokens)) | ||
(throw (js/Error. "no arithmetic expression found"))) | ||
(throw (js/Error. "cannot recognize question")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[88bf4b28-0de3-4883-93c7-db1b14aa806e] | ||
description = "just a number" | ||
|
||
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0] | ||
description = "addition" | ||
|
||
[79e49e06-c5ae-40aa-a352-7a3a01f70015] | ||
description = "more addition" | ||
|
||
[b345dbe0-f733-44e1-863c-5ae3568f3803] | ||
description = "addition with negative numbers" | ||
|
||
[cd070f39-c4cc-45c4-97fb-1be5e5846f87] | ||
description = "large addition" | ||
|
||
[0d86474a-cd93-4649-a4fa-f6109a011191] | ||
description = "subtraction" | ||
|
||
[30bc8395-5500-4712-a0cf-1d788a529be5] | ||
description = "multiplication" | ||
|
||
[34c36b08-8605-4217-bb57-9a01472c427f] | ||
description = "division" | ||
|
||
[da6d2ce4-fb94-4d26-8f5f-b078adad0596] | ||
description = "multiple additions" | ||
|
||
[7fd74c50-9911-4597-be09-8de7f2fea2bb] | ||
description = "addition and subtraction" | ||
|
||
[b120ffd5-bad6-4e22-81c8-5512e8faf905] | ||
description = "multiple subtraction" | ||
|
||
[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae] | ||
description = "subtraction then addition" | ||
|
||
[312d908c-f68f-42c9-aa75-961623cc033f] | ||
description = "multiple multiplication" | ||
|
||
[38e33587-8940-4cc1-bc28-bfd7e3966276] | ||
description = "addition and multiplication" | ||
|
||
[3c854f97-9311-46e8-b574-92b60d17d394] | ||
description = "multiple division" | ||
|
||
[3ad3e433-8af7-41ec-aa9b-97b42ab49357] | ||
description = "unknown operation" | ||
|
||
[8a7e85a8-9e7b-4d46-868f-6d759f4648f8] | ||
description = "Non math question" | ||
|
||
[42d78b5f-dbd7-4cdb-8b30-00f794bb24cf] | ||
description = "reject problem missing an operand" | ||
|
||
[c2c3cbfc-1a72-42f2-b597-246e617e66f5] | ||
description = "reject problem with no operands or operators" | ||
|
||
[4b3df66d-6ed5-4c95-a0a1-d38891fbdab6] | ||
description = "reject two operations in a row" | ||
|
||
[6abd7a50-75b4-4665-aa33-2030fd08bab1] | ||
description = "reject two numbers in a row" | ||
|
||
[10a56c22-e0aa-405f-b1d2-c642d9c4c9de] | ||
description = "reject postfix notation" | ||
|
||
[0035bc63-ac43-4bb5-ad6d-e8651b7d954e] | ||
description = "reject prefix notation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(ns wordy) | ||
|
||
(defn evaluate [] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
(ns wordy-test | ||
(:require [clojure.test :refer [deftest 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? | ||
js/Error | ||
(wordy/evaluate "What is 52 cubed?")))) | ||
|
||
(deftest Non-math-question | ||
(is (thrown? | ||
js/Error | ||
(wordy/evaluate "Who is the President of the United States?")))) | ||
|