Skip to content

Commit 8b195e7

Browse files
Add wordy exercise (#72)
1 parent fc4e8ef commit 8b195e7

File tree

7 files changed

+373
-0
lines changed

7 files changed

+373
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@
497497
"prerequisites": [],
498498
"difficulty": 8
499499
},
500+
{
501+
"slug": "wordy",
502+
"name": "Wordy",
503+
"uuid": "bcfdd937-5cbb-4ac3-80f9-818724d87c4c",
504+
"practices": [],
505+
"prerequisites": [],
506+
"difficulty": 8
507+
},
500508
{
501509
"slug": "zebra-puzzle",
502510
"name": "Zebra Puzzle",
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.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
def atoi [n] (word: [n]u8): i32 =
3+
let (sign, magnitude) = loop (sign, magnitude) = ('+', 0) for i < n - 1 do
4+
if i == 0 && word[i] == '-' then ('-', magnitude) else
5+
let digit = word[i] - '0'
6+
in
7+
assert (digit < 10) (sign, magnitude * 10 + i32.u8 digit)
8+
in
9+
if sign == '+' then magnitude else
10+
-magnitude
11+
12+
def equal [m] [n] (first: [m]u8) (second: [n]u8): bool =
13+
if m != n then false else
14+
let same = loop same = true for i < n do
15+
if first[i] == second[i] then same else
16+
false
17+
in
18+
same
19+
20+
def find_offsets [n] (question: [n]u8): []i64 =
21+
if length question == 0 then [] else
22+
let offsets = loop offsets = [0] for i < n do
23+
if question[i] != ' ' && question[i] != '?' then offsets else
24+
if i > 0 && (question[i - 1] == 'd' || question[i - 1] == 't') && (question[i] == ' ') then offsets else
25+
offsets ++ [i + 1]
26+
in
27+
offsets
28+
29+
def question_mark_at_end [n] (word: [n]u8): bool =
30+
let result = loop result = true for ch in word[0:n - 1] do
31+
result && ch != '?'
32+
in
33+
result && word[n - 1] == '?'
34+
35+
def calculate (first: i32) (operation: u8) (second: i32): i32 =
36+
match operation
37+
case '+' -> first + second
38+
case '-' -> first - second
39+
case '*' -> first * second
40+
case '/' -> first / second
41+
case _ -> assert false 0
42+
43+
def answer (question: []u8): i32 =
44+
let offsets = assert (question_mark_at_end question) (find_offsets question)
45+
in
46+
let (value, operation) = loop (value, operation) = (0, '^') for i < length offsets - 1 do
47+
let word = question[offsets[i]:offsets[i + 1]]
48+
in
49+
if operation == '^' && equal word "What is " then (0, '+') else
50+
if operation == '.' && equal word "plus " then (value, '+') else
51+
if operation == '.' && equal word "minus " then (value, '-') else
52+
if operation == '.' && equal word "multiplied by " then (value, '*') else
53+
if operation == '.' && equal word "divided by " then (value, '/') else
54+
assert (i % 2 == 1) (calculate value operation (atoi word), '.')
55+
in
56+
assert (operation == '.') value
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"

exercises/practice/wordy/test.fut

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import "wordy"
2+
3+
-- just a number
4+
-- ==
5+
-- input { "What is 5?" }
6+
-- output { 5 }
7+
8+
-- just a zero
9+
-- ==
10+
-- input { "What is 0?" }
11+
-- output { 0 }
12+
13+
-- just a negative number
14+
-- ==
15+
-- input { "What is -123?" }
16+
-- output { -123 }
17+
18+
-- addition
19+
-- ==
20+
-- input { "What is 1 plus 1?" }
21+
-- output { 2 }
22+
23+
-- addition with a left hand zero
24+
-- ==
25+
-- input { "What is 0 plus 2?" }
26+
-- output { 2 }
27+
28+
-- addition with a right hand zero
29+
-- ==
30+
-- input { "What is 3 plus 0?" }
31+
-- output { 3 }
32+
33+
-- more addition
34+
-- ==
35+
-- input { "What is 53 plus 2?" }
36+
-- output { 55 }
37+
38+
-- addition with negative numbers
39+
-- ==
40+
-- input { "What is -1 plus -10?" }
41+
-- output { -11 }
42+
43+
-- large addition
44+
-- ==
45+
-- input { "What is 123 plus 45678?" }
46+
-- output { 45801 }
47+
48+
-- subtraction
49+
-- ==
50+
-- input { "What is 4 minus -12?" }
51+
-- output { 16 }
52+
53+
-- multiplication
54+
-- ==
55+
-- input { "What is -3 multiplied by 25?" }
56+
-- output { -75 }
57+
58+
-- division
59+
-- ==
60+
-- input { "What is 33 divided by -3?" }
61+
-- output { -11 }
62+
63+
-- multiple additions
64+
-- ==
65+
-- input { "What is 1 plus 1 plus 1?" }
66+
-- output { 3 }
67+
68+
-- addition and subtraction
69+
-- ==
70+
-- input { "What is 1 plus 5 minus -2?" }
71+
-- output { 8 }
72+
73+
-- multiple subtraction
74+
-- ==
75+
-- input { "What is 20 minus 4 minus 13?" }
76+
-- output { 3 }
77+
78+
-- subtraction then addition
79+
-- ==
80+
-- input { "What is 17 minus 6 plus 3?" }
81+
-- output { 14 }
82+
83+
-- multiple multiplication
84+
-- ==
85+
-- input { "What is 2 multiplied by -2 multiplied by 3?" }
86+
-- output { -12 }
87+
88+
-- addition and multiplication
89+
-- ==
90+
-- input { "What is -3 plus 7 multiplied by -2?" }
91+
-- output { -8 }
92+
93+
-- multiple division
94+
-- ==
95+
-- input { "What is -12 divided by 2 divided by -3?" }
96+
-- output { 2 }
97+
98+
-- unknown operation
99+
-- ==
100+
-- input { "What is 52 cubed?" }
101+
-- error: Error*
102+
103+
-- Non math question
104+
-- ==
105+
-- input { "Who is the President of the United States?" }
106+
-- error: Error*
107+
108+
-- reject problem missing an operand
109+
-- ==
110+
-- input { "What is 1 plus?" }
111+
-- error: Error*
112+
113+
-- reject problem with no operands or operators
114+
-- ==
115+
-- input { "What is?" }
116+
-- error: Error*
117+
118+
-- reject two operations in a row
119+
-- ==
120+
-- input { "What is 1 plus plus 2?" }
121+
-- error: Error*
122+
123+
-- reject two numbers in a row
124+
-- ==
125+
-- input { "What is 1 plus 2 1?" }
126+
-- error: Error*
127+
128+
-- reject postfix notation
129+
-- ==
130+
-- input { "What is 1 2 plus?" }
131+
-- error: Error*
132+
133+
-- reject prefix notation
134+
-- ==
135+
-- input { "What is plus 1 2?" }
136+
-- error: Error*
137+
138+
let main (question: []u8): i32 =
139+
answer question

exercises/practice/wordy/wordy.fut

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def answer (question: []u8): i32 = ???

0 commit comments

Comments
 (0)