Skip to content

Commit f271281

Browse files
Add wordy (#487)
1 parent 7ab17f6 commit f271281

File tree

7 files changed

+349
-0
lines changed

7 files changed

+349
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,14 @@
955955
"prerequisites": [],
956956
"difficulty": 6
957957
},
958+
{
959+
"slug": "wordy",
960+
"name": "Wordy",
961+
"uuid": "4aab08ad-7c41-427c-966e-cd90f4dc7ba5",
962+
"practices": [],
963+
"prerequisites": [],
964+
"difficulty": 6
965+
},
958966
{
959967
"slug": "dominoes",
960968
"name": "Dominoes",
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+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"wordy.el"
8+
],
9+
"test": [
10+
"wordy-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
;;; wordy.el --- Wordy (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(define-error 'parse-error "unable to understand question")
8+
9+
(defun calculate (left tokens)
10+
(if (null tokens)
11+
left
12+
(let ((operation (car tokens)))
13+
(cond
14+
((null (cdr tokens)) (signal 'parse-error nil))
15+
((string= operation "plus") (calculate (+ left (string-to-number (cadr tokens))) (drop 2 tokens)))
16+
((string= operation "minus") (calculate (- left (string-to-number (cadr tokens))) (drop 2 tokens)))
17+
((string= operation "multiplied-by") (calculate (* left (string-to-number (cadr tokens))) (drop 2 tokens)))
18+
((string= operation "divided-by") (calculate (/ left (string-to-number (cadr tokens))) (drop 2 tokens)))
19+
(t (signal 'parse-error nil))))))
20+
21+
(defun answer (question)
22+
(unless (string= (substring question -1) "?") (signal 'parse-error nil))
23+
(let ((tokens (split-string (string-replace " by" "-by" (substring question 0 -1)))))
24+
(unless (string= (car tokens) "What") (signal 'parse-error nil))
25+
(unless (string= (cadr tokens) "is") (signal 'parse-error nil))
26+
(unless (cddr tokens) (signal 'parse-error nil))
27+
(calculate (string-to-number (caddr tokens)) (drop 3 tokens))))
28+
29+
30+
(provide 'wordy)
31+
;;; wordy.el ends here
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[88bf4b28-0de3-4883-93c7-db1b14aa806e]
13+
description = "just a number"
14+
15+
[18983214-1dfc-4ebd-ac77-c110dde699ce]
16+
description = "just a zero"
17+
18+
[607c08ee-2241-4288-916d-dae5455c87e6]
19+
description = "just a negative number"
20+
21+
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
22+
description = "addition"
23+
24+
[bb9f2082-171c-46ad-ad4e-c3f72087c1b5]
25+
description = "addition with a left hand zero"
26+
27+
[6fa05f17-405a-4742-80ae-5d1a8edb0d5d]
28+
description = "addition with a right hand zero"
29+
30+
[79e49e06-c5ae-40aa-a352-7a3a01f70015]
31+
description = "more addition"
32+
33+
[b345dbe0-f733-44e1-863c-5ae3568f3803]
34+
description = "addition with negative numbers"
35+
36+
[cd070f39-c4cc-45c4-97fb-1be5e5846f87]
37+
description = "large addition"
38+
39+
[0d86474a-cd93-4649-a4fa-f6109a011191]
40+
description = "subtraction"
41+
42+
[30bc8395-5500-4712-a0cf-1d788a529be5]
43+
description = "multiplication"
44+
45+
[34c36b08-8605-4217-bb57-9a01472c427f]
46+
description = "division"
47+
48+
[da6d2ce4-fb94-4d26-8f5f-b078adad0596]
49+
description = "multiple additions"
50+
51+
[7fd74c50-9911-4597-be09-8de7f2fea2bb]
52+
description = "addition and subtraction"
53+
54+
[b120ffd5-bad6-4e22-81c8-5512e8faf905]
55+
description = "multiple subtraction"
56+
57+
[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae]
58+
description = "subtraction then addition"
59+
60+
[312d908c-f68f-42c9-aa75-961623cc033f]
61+
description = "multiple multiplication"
62+
63+
[38e33587-8940-4cc1-bc28-bfd7e3966276]
64+
description = "addition and multiplication"
65+
66+
[3c854f97-9311-46e8-b574-92b60d17d394]
67+
description = "multiple division"
68+
69+
[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
70+
description = "unknown operation"
71+
72+
[8a7e85a8-9e7b-4d46-868f-6d759f4648f8]
73+
description = "Non math question"
74+
75+
[42d78b5f-dbd7-4cdb-8b30-00f794bb24cf]
76+
description = "reject problem missing an operand"
77+
78+
[c2c3cbfc-1a72-42f2-b597-246e617e66f5]
79+
description = "reject problem with no operands or operators"
80+
81+
[4b3df66d-6ed5-4c95-a0a1-d38891fbdab6]
82+
description = "reject two operations in a row"
83+
84+
[6abd7a50-75b4-4665-aa33-2030fd08bab1]
85+
description = "reject two numbers in a row"
86+
87+
[10a56c22-e0aa-405f-b1d2-c642d9c4c9de]
88+
description = "reject postfix notation"
89+
90+
[0035bc63-ac43-4bb5-ad6d-e8651b7d954e]
91+
description = "reject prefix notation"
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
;;; wordy-test.el --- Tests for Wordy (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;; For this exercise we don't require specific error strings,
6+
;; to avoid constraining solution approaches.
7+
8+
;;; Code:
9+
10+
11+
(load-file "wordy.el")
12+
(declare-function answer "wordy.el" (question))
13+
14+
15+
(ert-deftest just-a-number ()
16+
(should (= 5 (answer "What is 5?"))))
17+
18+
19+
(ert-deftest just-a-zero ()
20+
(should (= 0 (answer "What is 0?"))))
21+
22+
23+
(ert-deftest just-a-negative-number ()
24+
(should (= -123 (answer "What is -123?"))))
25+
26+
27+
(ert-deftest addition ()
28+
(should (= 2 (answer "What is 1 plus 1?"))))
29+
30+
31+
(ert-deftest addition-with-a-left-hand-zero ()
32+
(should (= 2 (answer "What is 0 plus 2?"))))
33+
34+
35+
(ert-deftest addition-with-a-right-hand-zero ()
36+
(should (= 3 (answer "What is 3 plus 0?"))))
37+
38+
39+
(ert-deftest more-addition ()
40+
(should (= 55 (answer "What is 53 plus 2?"))))
41+
42+
43+
(ert-deftest addition-with-negative-numbers ()
44+
(should (= -11 (answer "What is -1 plus -10?"))))
45+
46+
47+
(ert-deftest large-addition ()
48+
(should (= 45801 (answer "What is 123 plus 45678?"))))
49+
50+
51+
(ert-deftest subtraction ()
52+
(should (= 16 (answer "What is 4 minus -12?"))))
53+
54+
55+
(ert-deftest multiplication ()
56+
(should (= -75 (answer "What is -3 multiplied by 25?"))))
57+
58+
59+
(ert-deftest division ()
60+
(should (= -11 (answer "What is 33 divided by -3?"))))
61+
62+
63+
(ert-deftest multiple-additions ()
64+
(should (= 3 (answer "What is 1 plus 1 plus 1?"))))
65+
66+
67+
(ert-deftest addition-and-subtraction ()
68+
(should (= 8 (answer "What is 1 plus 5 minus -2?"))))
69+
70+
71+
(ert-deftest multiple-subtraction ()
72+
(should (= 3 (answer "What is 20 minus 4 minus 13?"))))
73+
74+
75+
(ert-deftest subtraction-then-addition ()
76+
(should (= 14 (answer "What is 17 minus 6 plus 3?"))))
77+
78+
79+
(ert-deftest multiple-multiplication ()
80+
(should (= -12 (answer "What is 2 multiplied by -2 multiplied by 3?"))))
81+
82+
83+
(ert-deftest addition-and-multiplication ()
84+
(should (= -8 (answer "What is -3 plus 7 multiplied by -2?"))))
85+
86+
87+
(ert-deftest multiple-division ()
88+
(should (= 2 (answer "What is -12 divided by 2 divided by -3?"))))
89+
90+
91+
(ert-deftest unknown-operation ()
92+
(should-error (answer "What is 52 cubed?") :type 'parse-error))
93+
94+
95+
(ert-deftest non-math-question ()
96+
(should-error (answer "Who is the President of the United States?") :type 'parse-error))
97+
98+
99+
(ert-deftest reject-problem-missing-an-operand ()
100+
(should-error (answer "What is 1 plus?") :type 'parse-error))
101+
102+
103+
(ert-deftest reject-problem-with-no-operands-or-operators ()
104+
(should-error (answer "What is?") :type 'parse-error))
105+
106+
107+
(ert-deftest reject-two-operations-in-a-row ()
108+
(should-error (answer "What is 1 plus plus 2?") :type 'parse-error))
109+
110+
111+
(ert-deftest reject-two-numbers-in-a-row ()
112+
(should-error (answer "What is 1 plus 2 1?") :type 'parse-error))
113+
114+
115+
(ert-deftest reject-postfix-notation ()
116+
(should-error (answer "What is 1 2 plus?") :type 'parse-error))
117+
118+
119+
(ert-deftest reject-prefix-notation ()
120+
(should-error (answer "What is plus 1 2?") :type 'parse-error))
121+
122+
123+
(provide 'wordy-test)
124+
;;; wordy-test.el ends here

exercises/practice/wordy/wordy.el

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;;; wordy.el --- Wordy (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(define-error 'parse-error
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(defun answer (question)
13+
(error "Delete this S-Expression and write your own implementation"))
14+
15+
16+
(provide 'wordy)
17+
;;; wordy.el ends here

0 commit comments

Comments
 (0)