Skip to content

Commit

Permalink
Sync exercise wordy with problem spec
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Sep 16, 2023
1 parent 7328ece commit 5488f14
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 70 deletions.
17 changes: 4 additions & 13 deletions exercises/practice/wordy/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ Now, perform the other three operations.

Handle a set of operations, in sequence.

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

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

The parser should reject:

* Unsupported operations ("What is 52 cubed?")
* Non-math questions ("Who is the President of the United States")
* Word problems with invalid syntax ("What is 1 plus plus 2?")

## Bonus — Exponentials

If you'd like, handle exponentials.

> What is 2 raised to the 5th power?
32
- Unsupported operations ("What is 52 cubed?")
- Non-math questions ("Who is the President of the United States")
- Word problems with invalid syntax ("What is 1 plus plus 2?")
16 changes: 16 additions & 0 deletions exercises/practice/wordy/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = {{ test.input | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let expected = {% if test.expected is object -%}
None
{%- else -%}
Some({{ test.expected }})
{%- endif %};
assert_eq!(output, expected);
}
{% endfor -%}
19 changes: 16 additions & 3 deletions exercises/practice/wordy/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

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

[312d908c-f68f-42c9-aa75-961623cc033f]
description = "multiple multiplication"

[38e33587-8940-4cc1-bc28-bfd7e3966276]
description = "addition and multiplication"

[3c854f97-9311-46e8-b574-92b60d17d394]
description = "multiple division"

[3ad3e433-8af7-41ec-aa9b-97b42ab49357]
description = "unknown operation"

Expand Down
157 changes: 103 additions & 54 deletions exercises/practice/wordy/tests/wordy.rs
Original file line number Diff line number Diff line change
@@ -1,177 +1,226 @@
use wordy::answer;

#![deny(warnings)]
#[test]
fn just_a_number() {
let command = "What is 5?";
assert_eq!(Some(5), answer(command));
let input = "What is 5?";
let output = wordy::answer(input);
let expected = Some(5);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn addition() {
let command = "What is 1 plus 1?";
assert_eq!(Some(2), answer(command));
let input = "What is 1 plus 1?";
let output = wordy::answer(input);
let expected = Some(2);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn more_addition() {
let command = "What is 53 plus 2?";
assert_eq!(Some(55), answer(command));
let input = "What is 53 plus 2?";
let output = wordy::answer(input);
let expected = Some(55);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn addition_with_negative_numbers() {
let command = "What is -1 plus -10?";
assert_eq!(Some(-11), answer(command));
let input = "What is -1 plus -10?";
let output = wordy::answer(input);
let expected = Some(-11);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn large_addition() {
let command = "What is 123 plus 45678?";
assert_eq!(Some(45_801), answer(command));
let input = "What is 123 plus 45678?";
let output = wordy::answer(input);
let expected = Some(45801);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn subtraction() {
let command = "What is 4 minus -12?";
assert_eq!(Some(16), answer(command));
let input = "What is 4 minus -12?";
let output = wordy::answer(input);
let expected = Some(16);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiplication() {
let command = "What is -3 multiplied by 25?";
assert_eq!(Some(-75), answer(command));
let input = "What is -3 multiplied by 25?";
let output = wordy::answer(input);
let expected = Some(-75);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn division() {
let command = "What is 33 divided by -3?";
assert_eq!(Some(-11), answer(command));
let input = "What is 33 divided by -3?";
let output = wordy::answer(input);
let expected = Some(-11);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_additions() {
let command = "What is 1 plus 1 plus 1?";
assert_eq!(Some(3), answer(command));
let input = "What is 1 plus 1 plus 1?";
let output = wordy::answer(input);
let expected = Some(3);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn addition_and_subtraction() {
let command = "What is 1 plus 5 minus -2?";
assert_eq!(Some(8), answer(command));
let input = "What is 1 plus 5 minus -2?";
let output = wordy::answer(input);
let expected = Some(8);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_subtraction() {
let command = "What is 20 minus 4 minus 13?";
assert_eq!(Some(3), answer(command));
let input = "What is 20 minus 4 minus 13?";
let output = wordy::answer(input);
let expected = Some(3);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn subtraction_then_addition() {
let command = "What is 17 minus 6 plus 3?";
assert_eq!(Some(14), answer(command));
let input = "What is 17 minus 6 plus 3?";
let output = wordy::answer(input);
let expected = Some(14);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_multiplications() {
let command = "What is 2 multiplied by -2 multiplied by 3?";
assert_eq!(Some(-12), answer(command));
fn multiple_multiplication() {
let input = "What is 2 multiplied by -2 multiplied by 3?";
let output = wordy::answer(input);
let expected = Some(-12);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn addition_and_multiplication() {
let command = "What is -3 plus 7 multiplied by -2?";
assert_eq!(Some(-8), answer(command));
let input = "What is -3 plus 7 multiplied by -2?";
let output = wordy::answer(input);
let expected = Some(-8);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn multiple_divisions() {
let command = "What is -12 divided by 2 divided by -3?";
assert_eq!(Some(2), answer(command));
fn multiple_division() {
let input = "What is -12 divided by 2 divided by -3?";
let output = wordy::answer(input);
let expected = Some(2);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn unknown_operation() {
let command = "What is 52 cubed?";
assert_eq!(None, answer(command));
let input = "What is 52 cubed?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn non_math_question() {
let command = "Who is the President of the United States?";
assert_eq!(None, answer(command));
let input = "Who is the President of the United States?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_problem_missing_an_operand() {
let command = "What is 1 plus?";
assert_eq!(None, answer(command));
let input = "What is 1 plus?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_problem_with_no_operands_or_operators() {
let command = "What is?";
assert_eq!(None, answer(command));
let input = "What is?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_two_operations_in_a_row() {
let command = "What is 1 plus plus 2?";
assert_eq!(None, answer(command));
let input = "What is 1 plus plus 2?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_two_numbers_in_a_row() {
let command = "What is 1 plus 2 1?";
assert_eq!(None, answer(command));
let input = "What is 1 plus 2 1?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_postfix_notation() {
let command = "What is 1 2 plus?";
assert_eq!(None, answer(command));
let input = "What is 1 2 plus?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn reject_prefix_notation() {
let command = "What is plus 1 2?";
assert_eq!(None, answer(command));
let input = "What is plus 1 2?";
let output = wordy::answer(input);
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
#[cfg(feature = "exponentials")]
fn exponential() {
let command = "What is 2 raised to the 5th power?";
assert_eq!(Some(32), answer(command));
let input = "What is 2 raised to the 5th power?";
let output = wordy::answer(input);
let expected = Some(32);
assert_eq!(output, expected);
}

#[test]
#[ignore]
#[cfg(feature = "exponentials")]
fn addition_and_exponential() {
let command = "What is 1 plus 2 raised to the 2nd power?";
assert_eq!(Some(9), answer(command));
let input = "What is 1 plus 2 raised to the 2nd power?";
let output = wordy::answer(input);
let expected = Some(9);
assert_eq!(output, expected);
}

0 comments on commit 5488f14

Please sign in to comment.