Skip to content

Commit b8ef84e

Browse files
Add wordy
1 parent 4c972d0 commit b8ef84e

File tree

7 files changed

+312
-0
lines changed

7 files changed

+312
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,14 @@
903903
"prerequisites": [],
904904
"difficulty": 6
905905
},
906+
{
907+
"slug": "wordy",
908+
"name": "Wordy",
909+
"uuid": "4aab08ad-7c41-427c-966e-cd90f4dc7ba5",
910+
"practices": [],
911+
"prerequisites": [],
912+
"difficulty": 6
913+
},
906914
{
907915
"slug": "knapsack",
908916
"name": "Knapsack",
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
;;; wordy.el --- Wordy (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(defun calculate (left tokens)
8+
(if (null tokens)
9+
left
10+
(let ((operation (car tokens)))
11+
(cond
12+
((string= operation "plus") (calculate (+ left (string-to-number (cadr tokens))) (drop 2 tokens)))
13+
((string= operation "minus") (calculate (- left (string-to-number (cadr tokens))) (drop 2 tokens)))
14+
((and (string= operation "multiplied") (string= (cadr tokens) "by")) (calculate (* left (string-to-number (caddr tokens))) (drop 3 tokens)))
15+
((and (string= operation "divided") (string= (cadr tokens) "by")) (calculate (/ left (string-to-number (caddr tokens))) (drop 3 tokens)))
16+
(t (error "unknown operation"))))))
17+
18+
(defun answer (question)
19+
(unless (string= (substring question -1) "?") (error "syntax error"))
20+
(let ((tokens (split-string (substring question 0 -1))))
21+
(unless (string= (car tokens) "What") (error "unknown operation"))
22+
(unless (string= (cadr tokens) "is") (error "unknown operation"))
23+
(calculate (string-to-number (caddr tokens)) (drop 3 tokens))))
24+
25+
26+
(provide 'wordy)
27+
;;; wordy.el ends here
28+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
[bb8c655c-cf42-4dfc-90e0-152fcfd8d4e0]
16+
description = "addition"
17+
18+
[79e49e06-c5ae-40aa-a352-7a3a01f70015]
19+
description = "more addition"
20+
21+
[b345dbe0-f733-44e1-863c-5ae3568f3803]
22+
description = "addition with negative numbers"
23+
24+
[cd070f39-c4cc-45c4-97fb-1be5e5846f87]
25+
description = "large addition"
26+
27+
[0d86474a-cd93-4649-a4fa-f6109a011191]
28+
description = "subtraction"
29+
30+
[30bc8395-5500-4712-a0cf-1d788a529be5]
31+
description = "multiplication"
32+
33+
[34c36b08-8605-4217-bb57-9a01472c427f]
34+
description = "division"
35+
36+
[da6d2ce4-fb94-4d26-8f5f-b078adad0596]
37+
description = "multiple additions"
38+
39+
[7fd74c50-9911-4597-be09-8de7f2fea2bb]
40+
description = "addition and subtraction"
41+
42+
[b120ffd5-bad6-4e22-81c8-5512e8faf905]
43+
description = "multiple subtraction"
44+
45+
[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae]
46+
description = "subtraction then addition"
47+
48+
[312d908c-f68f-42c9-aa75-961623cc033f]
49+
description = "multiple multiplication"
50+
51+
[38e33587-8940-4cc1-bc28-bfd7e3966276]
52+
description = "addition and multiplication"
53+
54+
[3c854f97-9311-46e8-b574-92b60d17d394]
55+
description = "multiple division"
56+
57+
[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
58+
description = "unknown operation"
59+
60+
[8a7e85a8-9e7b-4d46-868f-6d759f4648f8]
61+
description = "Non math question"
62+
63+
[42d78b5f-dbd7-4cdb-8b30-00f794bb24cf]
64+
description = "reject problem missing an operand"
65+
66+
[c2c3cbfc-1a72-42f2-b597-246e617e66f5]
67+
description = "reject problem with no operands or operators"
68+
69+
[4b3df66d-6ed5-4c95-a0a1-d38891fbdab6]
70+
description = "reject two operations in a row"
71+
72+
[6abd7a50-75b4-4665-aa33-2030fd08bab1]
73+
description = "reject two numbers in a row"
74+
75+
[10a56c22-e0aa-405f-b1d2-c642d9c4c9de]
76+
description = "reject postfix notation"
77+
78+
[0035bc63-ac43-4bb5-ad6d-e8651b7d954e]
79+
description = "reject prefix notation"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
;;; wordy-test.el --- Tests for Wordy (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "wordy.el")
9+
(declare-function answer "wordy.el" (question))
10+
11+
12+
(ert-deftest just-a-number ()
13+
(should (= 5 (answer "What is 5?"))))
14+
15+
16+
(ert-deftest addition ()
17+
(should (= 2 (answer "What is 1 plus 1?"))))
18+
19+
20+
(ert-deftest more-addition ()
21+
(should (= 55 (answer "What is 53 plus 2?"))))
22+
23+
24+
(ert-deftest addition-with-negative-numbers ()
25+
(should (= -11 (answer "What is -1 plus -10?"))))
26+
27+
28+
(ert-deftest large-addition ()
29+
(should (= 45801 (answer "What is 123 plus 45678?"))))
30+
31+
32+
(ert-deftest subtraction ()
33+
(should (= 16 (answer "What is 4 minus -12?"))))
34+
35+
36+
(ert-deftest multiplication ()
37+
(should (= -75 (answer "What is -3 multiplied by 25?"))))
38+
39+
40+
(ert-deftest division ()
41+
(should (= -11 (answer "What is 33 divided by -3?"))))
42+
43+
44+
(ert-deftest multiple-additions ()
45+
(should (= 3 (answer "What is 1 plus 1 plus 1?"))))
46+
47+
48+
(ert-deftest addition-and-subtraction ()
49+
(should (= 8 (answer "What is 1 plus 5 minus -2?"))))
50+
51+
52+
(ert-deftest multiple-subtraction ()
53+
(should (= 3 (answer "What is 20 minus 4 minus 13?"))))
54+
55+
56+
(ert-deftest subtraction-then-addition ()
57+
(should (= 14 (answer "What is 17 minus 6 plus 3?"))))
58+
59+
60+
(ert-deftest multiple-multiplication ()
61+
(should (= -12 (answer "What is 2 multiplied by -2 multiplied by 3?"))))
62+
63+
64+
(ert-deftest addition-and-multiplication ()
65+
(should (= -8 (answer "What is -3 plus 7 multiplied by -2?"))))
66+
67+
68+
(ert-deftest multiple-division ()
69+
(should (= 2 (answer "What is -12 divided by 2 divided by -3?"))))
70+
71+
72+
(ert-deftest unknown-operation ()
73+
(should-error (answer "What is 52 cubed?")))
74+
75+
76+
(ert-deftest non-math-question ()
77+
(should-error (answer "Who is the President of the United States?")))
78+
79+
80+
(ert-deftest reject-problem-missing-an-operand ()
81+
(should-error (answer "What is 1 plus?")))
82+
83+
84+
(ert-deftest reject-problem-with-no-operands-or-operators ()
85+
(should-error (answer "What is?")))
86+
87+
88+
(ert-deftest reject-two-operations-in-a-row ()
89+
(should-error (answer "What is 1 plus plus 2?")))
90+
91+
92+
(ert-deftest reject-two-numbers-in-a-row ()
93+
(should-error (answer "What is 1 plus 2 1?")))
94+
95+
96+
(ert-deftest reject-postfix-notation ()
97+
(should-error (answer "What is 1 2 plus?")))
98+
99+
100+
(ert-deftest reject-prefix-notation ()
101+
(should-error (answer "What is plus 1 2?")))
102+
103+
104+
(provide 'wordy-test)
105+
;;; wordy-test.el ends here

exercises/practice/wordy/wordy.el

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

0 commit comments

Comments
 (0)