迟钝孩子-Bob

1. Readme

鲍勃-bob

鲍伯是一个懒散的青少年.在谈话中,他的反应非常有限.

  • 鲍伯回答:”Sure.”,如果你问他一个问题.

  • 他回答:”Whoa, chill out!”,如果你对他大喊大叫.

  • 他回答”Calm down, I know what I’m doing!”,如果你大声问他问题.

  • 他说”Fine. Be that way!”,如果你喊他,而不说任何话.

  • 他回答”Whatever”,给剩下的对话

鲍勃的对话伙伴,在书面交流方面是一个纯粹主义者,并且总是遵循关于 英语句子标点 的通用规则.

Source

Inspired by the ‘Deaf Grandma’ exercise in Chris Pine’s Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

2. 开始你的表演

pub fn reply(message: &str) -> &str {
   unimplemented!("have Bob reply to the incoming message: {}", message)
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[test]
fn test_stating_something() {
   assert_eq!("Whatever.", reply("Tom-ay-to, tom-aaaah-to."));
}

#[test]
//#[ignore]
fn test_shouting() {
   assert_eq!("Whoa, chill out!", reply("WATCH OUT!"));
}

#[test]
//#[ignore]
fn test_shouting_gibberish() {
   assert_eq!("Whoa, chill out!", reply("FCECDFCAAB"));
}

#[test]
//#[ignore]
fn test_asking() {
   assert_eq!(
       "Sure.",
       reply("Does this cryogenic chamber make me look fat?")
   );
}

#[test]
//#[ignore]
fn test_ask_numeric_question() {
   assert_eq!("Sure.", reply("You are, what, like 15?"));
}

#[test]
//#[ignore]
fn test_asking_gibberish() {
   assert_eq!("Sure.", reply("fffbbcbeab?"));
}

#[test]
//#[ignore]
fn test_exclaiming() {
   assert_eq!("Whatever.", reply("Let's go make out behind the gym!"));
}

#[test]
//#[ignore]
fn test_using_acronyms_in_regular_speech() {
   assert_eq!(
       "Whatever.",
       reply("It's OK if you don't want to go to the DMV.")
   );
}

#[test]
//#[ignore]
fn test_forceful_question() {
   assert_eq!(
       "Calm down, I know what I'm doing!",
       reply("WHAT THE HELL WERE YOU THINKING?")
   );
}

#[test]
//#[ignore]
fn test_shouting_numbers() {
   assert_eq!("Whoa, chill out!", reply("1, 2, 3 GO!"));
}

#[test]
//#[ignore]
fn test_only_numbers() {
   assert_eq!("Whatever.", reply("1, 2, 3"));
}

#[test]
//#[ignore]
fn test_question_with_only_numbers() {
   assert_eq!("Sure.", reply("4?"));
}

#[test]
//#[ignore]
fn test_shouting_with_special_characters() {
   assert_eq!(
       "Whoa, chill out!",
       reply("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!")
   );
}

#[test]
//#[ignore]
fn test_shouting_with_no_exclamation_mark() {
   assert_eq!("Whoa, chill out!", reply("I HATE YOU"));
}

#[test]
//#[ignore]
fn test_statement_containing_question_mark() {
   assert_eq!("Whatever.", reply("Ending with ? means a question."));
}

#[test]
//#[ignore]
fn test_non_letters_with_question() {
   assert_eq!("Sure.", reply(":) ?"));
}

#[test]
//#[ignore]
fn test_prattling_on() {
   assert_eq!("Sure.", reply("Wait! Hang on. Are you going to be OK?"));
}

#[test]
//#[ignore]
fn test_silence() {
   assert_eq!("Fine. Be that way!", reply(""));
}

#[test]
//#[ignore]
fn test_prolonged_silence() {
   assert_eq!("Fine. Be that way!", reply("          "));
}

#[test]
//#[ignore]
fn test_alternate_silence() {
   assert_eq!("Fine. Be that way!", reply("\t\t\t\t\t\t\t\t\t\t"));
}

#[test]
//#[ignore]
fn test_multiple_line_question() {
   assert_eq!(
       "Whatever.",
       reply("\nDoes this cryogenic chamber make me look fat?\nno")
   );
}

#[test]
//#[ignore]
fn test_starting_with_whitespace() {
   assert_eq!("Whatever.", reply("         hmmmmmmm..."));
}

#[test]
//#[ignore]
fn test_ending_with_whitespace() {
   assert_eq!("Sure.", reply("Okay if like my  spacebar  quite a bit?   "));
}

#[test]
//#[ignore]
fn test_other_whitespace() {
   assert_eq!("Fine. Be that way!", reply("\n\r \t"));
}

#[test]
//#[ignore]
fn test_non_question_ending_with_whitespace() {
   assert_eq!(
       "Whatever.",
       reply("This is a statement ending with whitespace      ")
   );
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
pub fn reply(message: &str) -> &str {
   if is_silence(message) {
       "Fine. Be that way!"
   } else if is_yelling(message) && is_question(message) {
       "Calm down, I know what I'm doing!"
   } else if is_yelling(message) {
       "Whoa, chill out!"
   } else if is_question(message) {
       "Sure."
   } else {
       "Whatever."
   }
}

fn is_silence(message: &str) -> bool {
   message.trim().is_empty()
}

fn is_yelling(message: &str) -> bool {
   let s = message.trim_matches(|c: char| !c.is_alphabetic());
   !s.is_empty() && s.to_uppercase() == s
}

fn is_question(message: &str) -> bool {
   message.trim_right().ends_with("?")
}

#}



填充/相关