 # clinta's solution

## to Wordy in the Rust Track

Published at Sep 23 2019 · 0 comments
Instructions
Test suite
Solution

Parse and evaluate simple math word problems returning the answer as an integer.

## Iteration 0 — Numbers

Problems with no operations simply evaluate to the number given.

What is 5?

Evaluates to 5.

What is 5 plus 13?

Evaluates to 18.

Handle large numbers and negative numbers.

## Iteration 2 — Subtraction, Multiplication and Division

Now, perform the other three operations.

What is 7 minus 5?

2

What is 6 multiplied by 4?

24

What is 25 divided by 5?

5

## Iteration 3 — Multiple 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.

What is 5 plus 13 plus 6?

24

What is 3 plus 2 multiplied by 3?

15 (i.e. not 9)

## Iteration 4 — Errors

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

To get the bonus tests to run, execute the tests with:

``````\$ cargo test --features exponentials
``````

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ cargo test
``````

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the `tests` directory and remove the `#[ignore]` flag from the next test and get the tests to pass again. Each separate test is a function with `#[test]` flag above it. Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specific test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

``````cargo fmt
``````

To see, if your solution contains some common ineffective use cases, inside the solution directory use

``````cargo clippy --all-targets
``````

## Submitting the solution

Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.

## Feedback, Issues, Pull Requests

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

## Source

Inspired by one of the generated questions in the Extreme Startup game. https://github.com/rchatley/extreme_startup

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### wordy.rs

``````use wordy::answer;

#[test]
fn just_a_number() {
let command = "What is 5?";
}

#[test]
#[ignore]
let command = "What is 1 plus 1?";
}

#[test]
#[ignore]
let command = "What is 53 plus 2?";
}

#[test]
#[ignore]
let command = "What is -1 plus -10?";
}

#[test]
#[ignore]
let command = "What is 123 plus 45678?";
}

#[test]
#[ignore]
fn subtraction() {
let command = "What is 4 minus -12?";
}

#[test]
#[ignore]
fn multiplication() {
let command = "What is -3 multiplied by 25?";
}

#[test]
#[ignore]
fn division() {
let command = "What is 33 divided by -3?";
}

#[test]
#[ignore]
let command = "What is 1 plus 1 plus 1?";
}

#[test]
#[ignore]
let command = "What is 1 plus 5 minus -2?";
}

#[test]
#[ignore]
fn multiple_subtraction() {
let command = "What is 20 minus 4 minus 13?";
}

#[test]
#[ignore]
let command = "What is 17 minus 6 plus 3?";
}

#[test]
#[ignore]
fn multiple_multiplications() {
let command = "What is 2 multiplied by -2 multiplied by 3?";
}

#[test]
#[ignore]
let command = "What is -3 plus 7 multiplied by -2?";
}

#[test]
#[ignore]
fn multiple_divisions() {
let command = "What is -12 divided by 2 divided by -3?";
}

#[test]
#[ignore]
fn unknown_operation() {
let command = "What is 52 cubed?";
}

#[test]
#[ignore]
fn non_math_question() {
let command = "Who is the President of the United States?";
}

#[test]
#[ignore]
fn reject_problem_missing_an_operand() {
let command = "What is 1 plus?";
}

#[test]
#[ignore]
fn reject_problem_with_no_operands_or_operators() {
let command = "What is?";
}

#[test]
#[ignore]
fn reject_two_operations_in_a_row() {
let command = "What is 1 plus plus 2?";
}

#[test]
#[ignore]
fn reject_two_numbers_in_a_row() {
let command = "What is 1 plus 2 1?";
}

#[test]
#[ignore]
fn reject_postfix_notation() {
let command = "What is 1 2 plus?";
}

#[test]
#[ignore]
fn reject_prefix_notation() {
let command = "What is plus 1 2?";
}

#[test]
#[ignore]
#[cfg(feature="exponentials")]
fn exponential() {
let command = "What is 2 raised to the 5th power?";
}

#[test]
#[ignore]
#[cfg(feature="exponentials")]
let command = "What is 1 plus 2 raised to the 2nd power?";
}``````
``````pub struct WordProblem;

pub fn answer(command: &str) -> Option<i32> {
let command = command
.trim_start_matches("What is ")
.trim_end_matches("?")
.trim();

let mut words = command.split_whitespace();
let mut answer = next_num(&mut words)?;

while let Some(word) = words.next() {
"plus" => answer + next_num(&mut words)?,
"minus" => answer - next_num(&mut words)?,
"multiplied" => {
next_word_is(&mut words, "by")?;
}
"divided" => {
next_word_is(&mut words, "by")?;
}
"raised" => {
next_word_is(&mut words, "to")?;
next_word_is(&mut words, "the")?;
let n = words.next().and_then(|w| {
w.trim_end_matches(|c: char| !c.is_numeric())
.parse::<u32>()
.ok()
})?;
next_word_is(&mut words, "power")?;
}
_ => return None,
};
}

}

fn next_num(words: &mut std::str::SplitWhitespace) -> Option<i32> {
words.next().and_then(|w| w.parse::<i32>().ok())
}

fn next_word_is(words: &mut std::str::SplitWhitespace, word: &str) -> Option<()> {
words.next().filter(|w| w == &word).map(|_| ())
}``````