Wordy

1. Readme

罗唆

解析并运算简单的数学单词问题,将答案作为整数返回。

迭代 1 - 加法

将两个数字相加

What is 5 plus 13?

运算为 18.

处理大数和负数。

迭代 2 - 减法,乘法和除法

现在,执行其他三个操作.

What is 7 minus 5?

2

What is 6 multiplied by 4?

24

What is 25 divided by 5?

迭代 3 - 多个操作

按顺序处理一组操作.

由于这些是口头语言问题,从左到右运算表达式,忽略了典型的操作顺序(乘法优先级比加法高)。

What is 5 plus 13 plus 6?

24

What is 3 plus 2 multiplied by 3?

15(不是 9)

奖金 - 指数

如果你愿意,处理指数.

What is 2 raised to the 5th power?

32

资源

灵感来自 Extreme Startup 游戏中的一个生成的问题.https://github.com/rchatley/extreme_startup

2. 开始你的表演

pub struct WordProblem;

pub fn answer(command: &str) -> Option<i32> {
   unimplemented!(
       "Return the result of the command '{}' or None, if the command is invalid.",
       command
   );
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[test]
fn addition() {
   let command = "What is 1 plus 1?";
   assert_eq!(Some(2), answer(command));
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
struct Token<'a> {
   value: &'a str,
}

impl <'a> Token<'a> {
   fn is_valid(&self) -> bool {
       !self.value.is_empty() && (self.is_operand() || self.is_operator())
   }

   fn is_operand(&self) -> bool {
       self.value.chars().all(|c| c.is_numeric() || c == '-')
   }

   fn is_operator(&self) -> bool {
       self.value == "plus"
           || self.value == "minus"
           || self.value == "multiplied"
           || self.value == "divided"
   }
}

pub fn answer(c: &str) -> Option<i32> {
   let mut t = tokens(c);
   let mut result: i32 = 0;
   let mut opr = "plus";

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

fn evaluate(r: i32, operator: &str, operand: i32) -> i32 {
   match operator {
       "plus" => r + operand,
       "minus" => r - operand,
       "multiplied" => r * operand,
       "divided" => r / operand,
       _ => r,
   }
}

fn operand(t: &Token) -> i32 {
   t.value.parse().unwrap()
}

fn operator<'a>(t: &Token<'a>) -> &'a str {
   t.value
}

fn tokens<'a>(command: &'a str) -> Vec<Token<'a>> {
   command
       .split(|c: char| c.is_whitespace() || c == '?')
       .map(|w| Token {
           value: w,
       })
       .filter(|t| t.is_valid())
       .collect()
}

#}



填充/相关