Skip to content

wordy: Added template to the stub file #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/wordy/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ impl WordProblem {
}
}

pub fn answer(&self) -> Result<isize, ()> {
pub fn answer(&self) -> Option<isize> {
let mut t = self.tokens();
let mut result: isize = 0;
let mut opr = "plus".to_string();

if t.len() <= 1 {
Err(())
None
} else {
while t.len() > 1 {
result = self.evaluate(result, opr, self.operand(&t.remove(0)));
opr = self.operator(&t.remove(0));
}
result = self.evaluate(result, opr, self.operand(&t.remove(0)));
Ok(result)
Some(result)
}
}

Expand Down
15 changes: 15 additions & 0 deletions exercises/wordy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
pub struct WordProblem;

impl WordProblem {
pub fn new(command: &str) -> Self {
unimplemented!(
"From the '{}' command construct a new WordProblem struct.",
command
);
}

pub fn answer(&self) -> Option<i32> {
unimplemented!(
"Return the result of the transmitted command or None, if the command is invalid."
);
}
}
32 changes: 16 additions & 16 deletions exercises/wordy/tests/wordy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,110 @@ use wordy::*;
#[test]
fn addition() {
let command = "What is 1 plus 1?";
assert_eq!(Ok(2), WordProblem::new(command).answer());
assert_eq!(Some(2), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn more_addition() {
let command = "What is 53 plus 2?";
assert_eq!(Ok(55), WordProblem::new(command).answer());
assert_eq!(Some(55), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn addition_with_negative_numbers() {
let command = "What is -1 plus -10?";
assert_eq!(Ok(-11), WordProblem::new(command).answer());
assert_eq!(Some(-11), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn large_addition() {
let command = "What is 123 plus 45678?";
assert_eq!(Ok(45801), WordProblem::new(command).answer());
assert_eq!(Some(45801), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn subtraction() {
let command = "What is 4 minus -12?";
assert_eq!(Ok(16), WordProblem::new(command).answer());
assert_eq!(Some(16), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn multiplication() {
let command = "What is -3 multiplied by 25?";
assert_eq!(Ok(-75), WordProblem::new(command).answer());
assert_eq!(Some(-75), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn division() {
let command = "What is 33 divided by -3?";
assert_eq!(Ok(-11), WordProblem::new(command).answer());
assert_eq!(Some(-11), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn multiple_additions() {
let command = "What is 1 plus 1 plus 1?";
assert_eq!(Ok(3), WordProblem::new(command).answer());
assert_eq!(Some(3), WordProblem::new(command).answer());
}

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

#[test]
#[ignore]
fn multiple_subtraction() {
let command = "What is 20 minus 4 minus 13?";
assert_eq!(Ok(3), WordProblem::new(command).answer());
assert_eq!(Some(3), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn subtraction_then_addition() {
let command = "What is 17 minus 6 plus 3?";
assert_eq!(Ok(14), WordProblem::new(command).answer());
assert_eq!(Some(14), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn multiple_multiplications() {
let command = "What is 2 multiplied by -2 multiplied by 3?";
assert_eq!(Ok(-12), WordProblem::new(command).answer());
assert_eq!(Some(-12), WordProblem::new(command).answer());
}

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

#[test]
#[ignore]
fn multiple_divisions() {
let command = "What is -12 divided by 2 divided by -3?";
assert_eq!(Ok(2), WordProblem::new(command).answer());
assert_eq!(Some(2), WordProblem::new(command).answer());
}

#[test]
#[ignore]
fn unknown_operation() {
let command = "What is 52 cubed?";
assert!(WordProblem::new(command).answer().is_err());
assert!(WordProblem::new(command).answer().is_none());
}

#[test]
#[ignore]
fn non_math_question() {
let command = "Who is the President of the United States?";
assert!(WordProblem::new(command).answer().is_err());
assert!(WordProblem::new(command).answer().is_none());
}