Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit 4a39a21

Browse files
Add wordy exercise (#157)
1 parent fff71b7 commit 4a39a21

File tree

8 files changed

+260
-0
lines changed

8 files changed

+260
-0
lines changed

config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,22 @@
585585
],
586586
"difficulty": 2
587587
},
588+
{
589+
"slug": "wordy",
590+
"name": "Wordy",
591+
"uuid": "fae36fc2-9c03-42a0-b1a1-3d1dd61783ba",
592+
"practices": [
593+
"numbers",
594+
"strings",
595+
"conditionals"
596+
],
597+
"prerequisites": [
598+
"numbers",
599+
"strings",
600+
"conditionals"
601+
],
602+
"difficulty": 6
603+
},
588604
{
589605
"slug": "perfect-numbers",
590606
"name": "Perfect Numbers",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Instructions
2+
3+
Parse and evaluate simple math word problems returning the answer as an integer.
4+
5+
## Iteration 0 — Numbers
6+
7+
Problems with no operations simply evaluate to the number given.
8+
9+
> What is 5?
10+
11+
Evaluates to 5.
12+
13+
## Iteration 1 — Addition
14+
15+
Add two numbers together.
16+
17+
> What is 5 plus 13?
18+
19+
Evaluates to 18.
20+
21+
Handle large numbers and negative numbers.
22+
23+
## Iteration 2 — Subtraction, Multiplication and Division
24+
25+
Now, perform the other three operations.
26+
27+
> What is 7 minus 5?
28+
29+
2
30+
31+
> What is 6 multiplied by 4?
32+
33+
24
34+
35+
> What is 25 divided by 5?
36+
37+
5
38+
39+
## Iteration 3 — Multiple Operations
40+
41+
Handle a set of operations, in sequence.
42+
43+
Since these are verbal word problems, evaluate the expression from left-to-right, _ignoring the typical order of operations._
44+
45+
> What is 5 plus 13 plus 6?
46+
47+
24
48+
49+
> What is 3 plus 2 multiplied by 3?
50+
51+
15 (i.e. not 9)
52+
53+
## Iteration 4 — Errors
54+
55+
The parser should reject:
56+
57+
- Unsupported operations ("What is 52 cubed?")
58+
- Non-math questions ("Who is the President of the United States")
59+
- Word problems with invalid syntax ("What is 1 plus plus 2?")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/wordy.cljs"
8+
],
9+
"test": [
10+
"test/wordy_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
14+
]
15+
},
16+
"blurb": "Parse and evaluate simple math word problems returning the answer as an integer.",
17+
"source": "Inspired by one of the generated questions in the Extreme Startup game.",
18+
"source_url": "https://github.com/rchatley/extreme_startup"
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(ns wordy
2+
(:require [clojure.string :refer [join]]))
3+
4+
(def ^:private ops {"plus" +
5+
"minus" -
6+
"multiplied by" *
7+
"divided by" /})
8+
9+
(def ^:private tokens-pattern (re-pattern
10+
(str (join "|" (keys ops)) "|-?\\d+|\\S+")))
11+
12+
(defn- parse-op [op-str]
13+
(or (ops op-str)
14+
(throw (js/Error. (str "unknown operator " op-str)))))
15+
16+
(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 (js/parseInt x)))
21+
(js/parseInt token) (partition-all 2 tokens))
22+
(throw (js/Error. "no arithmetic expression found")))
23+
(throw (js/Error. "cannot recognize question"))))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[88bf4b28-0de3-4883-93c7-db1b14aa806e]
6+
description = "just a number"
7+
8+
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
9+
description = "addition"
10+
11+
[79e49e06-c5ae-40aa-a352-7a3a01f70015]
12+
description = "more addition"
13+
14+
[b345dbe0-f733-44e1-863c-5ae3568f3803]
15+
description = "addition with negative numbers"
16+
17+
[cd070f39-c4cc-45c4-97fb-1be5e5846f87]
18+
description = "large addition"
19+
20+
[0d86474a-cd93-4649-a4fa-f6109a011191]
21+
description = "subtraction"
22+
23+
[30bc8395-5500-4712-a0cf-1d788a529be5]
24+
description = "multiplication"
25+
26+
[34c36b08-8605-4217-bb57-9a01472c427f]
27+
description = "division"
28+
29+
[da6d2ce4-fb94-4d26-8f5f-b078adad0596]
30+
description = "multiple additions"
31+
32+
[7fd74c50-9911-4597-be09-8de7f2fea2bb]
33+
description = "addition and subtraction"
34+
35+
[b120ffd5-bad6-4e22-81c8-5512e8faf905]
36+
description = "multiple subtraction"
37+
38+
[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae]
39+
description = "subtraction then addition"
40+
41+
[312d908c-f68f-42c9-aa75-961623cc033f]
42+
description = "multiple multiplication"
43+
44+
[38e33587-8940-4cc1-bc28-bfd7e3966276]
45+
description = "addition and multiplication"
46+
47+
[3c854f97-9311-46e8-b574-92b60d17d394]
48+
description = "multiple division"
49+
50+
[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
51+
description = "unknown operation"
52+
53+
[8a7e85a8-9e7b-4d46-868f-6d759f4648f8]
54+
description = "Non math question"
55+
56+
[42d78b5f-dbd7-4cdb-8b30-00f794bb24cf]
57+
description = "reject problem missing an operand"
58+
59+
[c2c3cbfc-1a72-42f2-b597-246e617e66f5]
60+
description = "reject problem with no operands or operators"
61+
62+
[4b3df66d-6ed5-4c95-a0a1-d38891fbdab6]
63+
description = "reject two operations in a row"
64+
65+
[6abd7a50-75b4-4665-aa33-2030fd08bab1]
66+
description = "reject two numbers in a row"
67+
68+
[10a56c22-e0aa-405f-b1d2-c642d9c4c9de]
69+
description = "reject postfix notation"
70+
71+
[0035bc63-ac43-4bb5-ad6d-e8651b7d954e]
72+
description = "reject prefix notation"

exercises/practice/wordy/deps.edn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns wordy)
2+
3+
(defn evaluate [] ;; <- arglist goes here
4+
;; your code goes here
5+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
(ns wordy-test
2+
(:require [clojure.test :refer [deftest is]]
3+
wordy))
4+
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+
js/Error
50+
(wordy/evaluate "What is 52 cubed?"))))
51+
52+
(deftest Non-math-question
53+
(is (thrown?
54+
js/Error
55+
(wordy/evaluate "Who is the President of the United States?"))))
56+

0 commit comments

Comments
 (0)