Skip to content

Commit 5cf04a4

Browse files
authored
Add wordy practice exercise (#503)
1 parent 60ec6bf commit 5cf04a4

File tree

8 files changed

+315
-0
lines changed

8 files changed

+315
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,14 @@
10421042
"text_formatting"
10431043
]
10441044
},
1045+
{
1046+
"slug": "wordy",
1047+
"name": "Wordy",
1048+
"uuid": "37165dd0-bb19-4606-acfb-2a382c8bb274",
1049+
"practices": [],
1050+
"prerequisites": [],
1051+
"difficulty": 4
1052+
},
10451053
{
10461054
"slug": "rational-numbers",
10471055
"name": "Rational Numbers",

exercises/practice/wordy/.busted

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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?")
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": ["ryanplusplus"],
3+
"files": {
4+
"solution": [
5+
"wordy.lua"
6+
],
7+
"test": [
8+
"wordy_spec.lua"
9+
],
10+
"example": [
11+
".meta/example.lua"
12+
]
13+
},
14+
"blurb": "Parse and evaluate simple math word problems returning the answer as an integer.",
15+
"source": "Inspired by one of the generated questions in the Extreme Startup game.",
16+
"source_url": "https://github.com/rchatley/extreme_startup"
17+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local function answer(question)
2+
local accumulator
3+
4+
local remaining = question:gsub('%s*(%D+)%s+([-%d]+)', function(operator, operand)
5+
operand = assert(tonumber(operand))
6+
7+
if operator == 'What is' then
8+
accumulator = operand
9+
elseif operator == 'plus' then
10+
accumulator = accumulator + operand
11+
elseif operator == 'minus' then
12+
accumulator = accumulator - operand
13+
elseif operator == 'multiplied by' then
14+
accumulator = accumulator * operand
15+
elseif operator == 'divided by' then
16+
accumulator = accumulator / operand
17+
else
18+
error('Invalid question')
19+
end
20+
21+
return ''
22+
end)
23+
24+
if remaining ~= '?' then
25+
error('Invalid question')
26+
end
27+
28+
return accumulator
29+
end
30+
31+
return { answer = answer }
+79
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"

exercises/practice/wordy/wordy.lua

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local function answer(question)
2+
3+
end
4+
5+
return { answer = answer }
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
local wordy = require('wordy')
2+
3+
describe('wordy', function()
4+
it('just a number', function()
5+
assert.are.same(5, wordy.answer('What is 5?'))
6+
end)
7+
8+
it('addition', function()
9+
assert.are.same(2, wordy.answer('What is 1 plus 1?'))
10+
end)
11+
12+
it('more addition', function()
13+
assert.are.same(55, wordy.answer('What is 53 plus 2?'))
14+
end)
15+
16+
it('addition with negative numbers', function()
17+
assert.are.same(-11, wordy.answer('What is -1 plus -10?'))
18+
end)
19+
20+
it('large addition', function()
21+
assert.are.same(45801, wordy.answer('What is 123 plus 45678?'))
22+
end)
23+
24+
it('subtraction', function()
25+
assert.are.same(16, wordy.answer('What is 4 minus -12?'))
26+
end)
27+
28+
it('multiplication', function()
29+
assert.are.same(-75, wordy.answer('What is -3 multiplied by 25?'))
30+
end)
31+
32+
it('division', function()
33+
assert.are.same(-11, wordy.answer('What is 33 divided by -3?'))
34+
end)
35+
36+
it('multiple additions', function()
37+
assert.are.same(3, wordy.answer('What is 1 plus 1 plus 1?'))
38+
end)
39+
40+
it('addition and subtraction', function()
41+
assert.are.same(8, wordy.answer('What is 1 plus 5 minus -2?'))
42+
end)
43+
44+
it('multiple subtraction', function()
45+
assert.are.same(3, wordy.answer('What is 20 minus 4 minus 13?'))
46+
end)
47+
48+
it('subtraction then addition', function()
49+
assert.are.same(14, wordy.answer('What is 17 minus 6 plus 3?'))
50+
end)
51+
52+
it('multiple multiplication', function()
53+
assert.are.same(-12, wordy.answer('What is 2 multiplied by -2 multiplied by 3?'))
54+
end)
55+
56+
it('addition and multiplication', function()
57+
assert.are.same(-8, wordy.answer('What is -3 plus 7 multiplied by -2?'))
58+
end)
59+
60+
it('multiple division', function()
61+
assert.are.same(2, wordy.answer('What is -12 divided by 2 divided by -3?'))
62+
end)
63+
64+
it('unknown operation', function()
65+
assert.has_error(function()
66+
wordy.answer('What is 52 cubed?')
67+
end, 'Invalid question')
68+
end)
69+
70+
it('Non math question', function()
71+
assert.has_error(function()
72+
wordy.answer('Who is the President of the United States?')
73+
end, 'Invalid question')
74+
end)
75+
76+
it('reject problem missing an operand', function()
77+
assert.has_error(function()
78+
wordy.answer('What is 1 plus?')
79+
end, 'Invalid question')
80+
end)
81+
82+
it('reject problem with no operands or operators', function()
83+
assert.has_error(function()
84+
wordy.answer('What is?')
85+
end, 'Invalid question')
86+
end)
87+
88+
it('reject two operations in a row', function()
89+
assert.has_error(function()
90+
wordy.answer('What is 1 plus plus 2?')
91+
end, 'Invalid question')
92+
end)
93+
94+
it('reject two numbers in a row', function()
95+
assert.has_error(function()
96+
wordy.answer('What is 1 plus 2 1?')
97+
end, 'Invalid question')
98+
end)
99+
100+
it('reject postfix notation', function()
101+
assert.has_error(function()
102+
wordy.answer('What is 1 2 plus?')
103+
end, 'Invalid question')
104+
end)
105+
106+
it('reject prefix notation', function()
107+
assert.has_error(function()
108+
wordy.answer('What is plus 1 2?')
109+
end, 'Invalid question')
110+
end)
111+
end)

0 commit comments

Comments
 (0)