Avatar of epequeno

epequeno's solution

to Bob in the Rust Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

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 wich 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 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 specfic test is ignored use:

$ cargo test some_test -- --ignored

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

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 the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

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

bob.rs

extern crate bob;

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

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

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

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

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

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

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

#[test]
#[ignore]
fn test_using_acronyms_in_regular_speech() {
    assert_eq!(
        "Whatever.",
        bob::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!",
        bob::reply("WHAT THE HELL WERE YOU THINKING?")
    );
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#[test]
#[ignore]
fn test_non_question_ending_with_whitespace() {
    assert_eq!(
        "Whatever.",
        bob::reply("This is a statement ending with whitespace      ")
    );
}
pub fn reply(msg: &'static str) -> String {
    let mut res = String::new();
    if msg.ends_with("?") {
        res.push_str("Sure.")
    } else if msg == "" {
        res.push_str("Fine. Be that way!")
    } else if msg == msg.to_uppercase() {
        res.push_str("Whoa, chill out!")
    } else {
        res.push_str("Whatever.")
    }
    res
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?