Skip to content

Commit 7f2d256

Browse files
authored
Sync exercise wordy with problem spec (#1728)
1 parent 7328ece commit 7f2d256

4 files changed

Lines changed: 138 additions & 70 deletions

File tree

exercises/practice/wordy/.docs/instructions.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ Now, perform the other three operations.
4040

4141
Handle a set of operations, in sequence.
4242

43-
Since these are verbal word problems, evaluate the expression from
44-
left-to-right, _ignoring the typical order of operations._
43+
Since these are verbal word problems, evaluate the expression from left-to-right, _ignoring the typical order of operations._
4544

4645
> What is 5 plus 13 plus 6?
4746
@@ -55,14 +54,6 @@ left-to-right, _ignoring the typical order of operations._
5554

5655
The parser should reject:
5756

58-
* Unsupported operations ("What is 52 cubed?")
59-
* Non-math questions ("Who is the President of the United States")
60-
* Word problems with invalid syntax ("What is 1 plus plus 2?")
61-
62-
## Bonus — Exponentials
63-
64-
If you'd like, handle exponentials.
65-
66-
> What is 2 raised to the 5th power?
67-
68-
32
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% for test in cases %}
2+
#[test]
3+
{% if loop.index != 1 -%}
4+
#[ignore]
5+
{% endif -%}
6+
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
7+
let input = {{ test.input | json_encode() }};
8+
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
9+
let expected = {% if test.expected is object -%}
10+
None
11+
{%- else -%}
12+
Some({{ test.expected }})
13+
{%- endif %};
14+
assert_eq!(output, expected);
15+
}
16+
{% endfor -%}

exercises/practice/wordy/.meta/tests.toml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
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.
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.
411

512
[88bf4b28-0de3-4883-93c7-db1b14aa806e]
613
description = "just a number"
@@ -38,9 +45,15 @@ description = "multiple subtraction"
3845
[4f4a5749-ef0c-4f60-841f-abcfaf05d2ae]
3946
description = "subtraction then addition"
4047

48+
[312d908c-f68f-42c9-aa75-961623cc033f]
49+
description = "multiple multiplication"
50+
4151
[38e33587-8940-4cc1-bc28-bfd7e3966276]
4252
description = "addition and multiplication"
4353

54+
[3c854f97-9311-46e8-b574-92b60d17d394]
55+
description = "multiple division"
56+
4457
[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
4558
description = "unknown operation"
4659

Lines changed: 102 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,225 @@
1-
use wordy::answer;
2-
31
#[test]
42
fn just_a_number() {
5-
let command = "What is 5?";
6-
assert_eq!(Some(5), answer(command));
3+
let input = "What is 5?";
4+
let output = wordy::answer(input);
5+
let expected = Some(5);
6+
assert_eq!(output, expected);
77
}
88

99
#[test]
1010
#[ignore]
1111
fn addition() {
12-
let command = "What is 1 plus 1?";
13-
assert_eq!(Some(2), answer(command));
12+
let input = "What is 1 plus 1?";
13+
let output = wordy::answer(input);
14+
let expected = Some(2);
15+
assert_eq!(output, expected);
1416
}
1517

1618
#[test]
1719
#[ignore]
1820
fn more_addition() {
19-
let command = "What is 53 plus 2?";
20-
assert_eq!(Some(55), answer(command));
21+
let input = "What is 53 plus 2?";
22+
let output = wordy::answer(input);
23+
let expected = Some(55);
24+
assert_eq!(output, expected);
2125
}
2226

2327
#[test]
2428
#[ignore]
2529
fn addition_with_negative_numbers() {
26-
let command = "What is -1 plus -10?";
27-
assert_eq!(Some(-11), answer(command));
30+
let input = "What is -1 plus -10?";
31+
let output = wordy::answer(input);
32+
let expected = Some(-11);
33+
assert_eq!(output, expected);
2834
}
2935

3036
#[test]
3137
#[ignore]
3238
fn large_addition() {
33-
let command = "What is 123 plus 45678?";
34-
assert_eq!(Some(45_801), answer(command));
39+
let input = "What is 123 plus 45678?";
40+
let output = wordy::answer(input);
41+
let expected = Some(45801);
42+
assert_eq!(output, expected);
3543
}
3644

3745
#[test]
3846
#[ignore]
3947
fn subtraction() {
40-
let command = "What is 4 minus -12?";
41-
assert_eq!(Some(16), answer(command));
48+
let input = "What is 4 minus -12?";
49+
let output = wordy::answer(input);
50+
let expected = Some(16);
51+
assert_eq!(output, expected);
4252
}
4353

4454
#[test]
4555
#[ignore]
4656
fn multiplication() {
47-
let command = "What is -3 multiplied by 25?";
48-
assert_eq!(Some(-75), answer(command));
57+
let input = "What is -3 multiplied by 25?";
58+
let output = wordy::answer(input);
59+
let expected = Some(-75);
60+
assert_eq!(output, expected);
4961
}
5062

5163
#[test]
5264
#[ignore]
5365
fn division() {
54-
let command = "What is 33 divided by -3?";
55-
assert_eq!(Some(-11), answer(command));
66+
let input = "What is 33 divided by -3?";
67+
let output = wordy::answer(input);
68+
let expected = Some(-11);
69+
assert_eq!(output, expected);
5670
}
5771

5872
#[test]
5973
#[ignore]
6074
fn multiple_additions() {
61-
let command = "What is 1 plus 1 plus 1?";
62-
assert_eq!(Some(3), answer(command));
75+
let input = "What is 1 plus 1 plus 1?";
76+
let output = wordy::answer(input);
77+
let expected = Some(3);
78+
assert_eq!(output, expected);
6379
}
6480

6581
#[test]
6682
#[ignore]
6783
fn addition_and_subtraction() {
68-
let command = "What is 1 plus 5 minus -2?";
69-
assert_eq!(Some(8), answer(command));
84+
let input = "What is 1 plus 5 minus -2?";
85+
let output = wordy::answer(input);
86+
let expected = Some(8);
87+
assert_eq!(output, expected);
7088
}
7189

7290
#[test]
7391
#[ignore]
7492
fn multiple_subtraction() {
75-
let command = "What is 20 minus 4 minus 13?";
76-
assert_eq!(Some(3), answer(command));
93+
let input = "What is 20 minus 4 minus 13?";
94+
let output = wordy::answer(input);
95+
let expected = Some(3);
96+
assert_eq!(output, expected);
7797
}
7898

7999
#[test]
80100
#[ignore]
81101
fn subtraction_then_addition() {
82-
let command = "What is 17 minus 6 plus 3?";
83-
assert_eq!(Some(14), answer(command));
102+
let input = "What is 17 minus 6 plus 3?";
103+
let output = wordy::answer(input);
104+
let expected = Some(14);
105+
assert_eq!(output, expected);
84106
}
85107

86108
#[test]
87109
#[ignore]
88-
fn multiple_multiplications() {
89-
let command = "What is 2 multiplied by -2 multiplied by 3?";
90-
assert_eq!(Some(-12), answer(command));
110+
fn multiple_multiplication() {
111+
let input = "What is 2 multiplied by -2 multiplied by 3?";
112+
let output = wordy::answer(input);
113+
let expected = Some(-12);
114+
assert_eq!(output, expected);
91115
}
92116

93117
#[test]
94118
#[ignore]
95119
fn addition_and_multiplication() {
96-
let command = "What is -3 plus 7 multiplied by -2?";
97-
assert_eq!(Some(-8), answer(command));
120+
let input = "What is -3 plus 7 multiplied by -2?";
121+
let output = wordy::answer(input);
122+
let expected = Some(-8);
123+
assert_eq!(output, expected);
98124
}
99125

100126
#[test]
101127
#[ignore]
102-
fn multiple_divisions() {
103-
let command = "What is -12 divided by 2 divided by -3?";
104-
assert_eq!(Some(2), answer(command));
128+
fn multiple_division() {
129+
let input = "What is -12 divided by 2 divided by -3?";
130+
let output = wordy::answer(input);
131+
let expected = Some(2);
132+
assert_eq!(output, expected);
105133
}
106134

107135
#[test]
108136
#[ignore]
109137
fn unknown_operation() {
110-
let command = "What is 52 cubed?";
111-
assert_eq!(None, answer(command));
138+
let input = "What is 52 cubed?";
139+
let output = wordy::answer(input);
140+
let expected = None;
141+
assert_eq!(output, expected);
112142
}
113143

114144
#[test]
115145
#[ignore]
116146
fn non_math_question() {
117-
let command = "Who is the President of the United States?";
118-
assert_eq!(None, answer(command));
147+
let input = "Who is the President of the United States?";
148+
let output = wordy::answer(input);
149+
let expected = None;
150+
assert_eq!(output, expected);
119151
}
120152

121153
#[test]
122154
#[ignore]
123155
fn reject_problem_missing_an_operand() {
124-
let command = "What is 1 plus?";
125-
assert_eq!(None, answer(command));
156+
let input = "What is 1 plus?";
157+
let output = wordy::answer(input);
158+
let expected = None;
159+
assert_eq!(output, expected);
126160
}
127161

128162
#[test]
129163
#[ignore]
130164
fn reject_problem_with_no_operands_or_operators() {
131-
let command = "What is?";
132-
assert_eq!(None, answer(command));
165+
let input = "What is?";
166+
let output = wordy::answer(input);
167+
let expected = None;
168+
assert_eq!(output, expected);
133169
}
134170

135171
#[test]
136172
#[ignore]
137173
fn reject_two_operations_in_a_row() {
138-
let command = "What is 1 plus plus 2?";
139-
assert_eq!(None, answer(command));
174+
let input = "What is 1 plus plus 2?";
175+
let output = wordy::answer(input);
176+
let expected = None;
177+
assert_eq!(output, expected);
140178
}
141179

142180
#[test]
143181
#[ignore]
144182
fn reject_two_numbers_in_a_row() {
145-
let command = "What is 1 plus 2 1?";
146-
assert_eq!(None, answer(command));
183+
let input = "What is 1 plus 2 1?";
184+
let output = wordy::answer(input);
185+
let expected = None;
186+
assert_eq!(output, expected);
147187
}
148188

149189
#[test]
150190
#[ignore]
151191
fn reject_postfix_notation() {
152-
let command = "What is 1 2 plus?";
153-
assert_eq!(None, answer(command));
192+
let input = "What is 1 2 plus?";
193+
let output = wordy::answer(input);
194+
let expected = None;
195+
assert_eq!(output, expected);
154196
}
155197

156198
#[test]
157199
#[ignore]
158200
fn reject_prefix_notation() {
159-
let command = "What is plus 1 2?";
160-
assert_eq!(None, answer(command));
201+
let input = "What is plus 1 2?";
202+
let output = wordy::answer(input);
203+
let expected = None;
204+
assert_eq!(output, expected);
161205
}
162206

163207
#[test]
164208
#[ignore]
165209
#[cfg(feature = "exponentials")]
166210
fn exponential() {
167-
let command = "What is 2 raised to the 5th power?";
168-
assert_eq!(Some(32), answer(command));
211+
let input = "What is 2 raised to the 5th power?";
212+
let output = wordy::answer(input);
213+
let expected = Some(32);
214+
assert_eq!(output, expected);
169215
}
170216

171217
#[test]
172218
#[ignore]
173219
#[cfg(feature = "exponentials")]
174220
fn addition_and_exponential() {
175-
let command = "What is 1 plus 2 raised to the 2nd power?";
176-
assert_eq!(Some(9), answer(command));
221+
let input = "What is 1 plus 2 raised to the 2nd power?";
222+
let output = wordy::answer(input);
223+
let expected = Some(9);
224+
assert_eq!(output, expected);
177225
}

0 commit comments

Comments
 (0)