Bob is a lackadaisical teenager. In conversation, his responses are very limited.
Bob answers 'Sure.' if you ask him a question, such as "How are you?".
He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals).
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.
Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English.
Refer to the exercism help page for Rust installation and learning resources.
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 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
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.
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
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.
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.
Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
use bob;
fn process_response_case(phrase: &str, expected_response: &str) {
assert_eq!(bob::reply(phrase), expected_response);
}
#[test]
fn test_stating_something() {
process_response_case("Tom-ay-to, tom-aaaah-to.", "Whatever.");
}
#[test]
#[ignore]
fn test_shouting() {
process_response_case("WATCH OUT!", "Whoa, chill out!");
}
#[test]
#[ignore]
fn test_shouting_gibberish() {
process_response_case("FCECDFCAAB", "Whoa, chill out!");
}
#[test]
#[ignore]
fn test_asking_a_question() {
process_response_case("Does this cryogenic chamber make me look fat?", "Sure.");
}
#[test]
#[ignore]
fn test_asking_a_numeric_question() {
process_response_case("You are, what, like 15?", "Sure.");
}
#[test]
#[ignore]
fn test_asking_gibberish() {
process_response_case("fffbbcbeab?", "Sure.");
}
#[test]
#[ignore]
fn test_talking_forcefully() {
process_response_case("Let's go make out behind the gym!", "Whatever.");
}
#[test]
#[ignore]
fn test_using_acronyms_in_regular_speech() {
process_response_case("It's OK if you don't want to go to the DMV.", "Whatever.");
}
#[test]
#[ignore]
fn test_forceful_question() {
process_response_case(
"WHAT THE HELL WERE YOU THINKING?",
"Calm down, I know what I'm doing!",
);
}
#[test]
#[ignore]
fn test_shouting_numbers() {
process_response_case("1, 2, 3 GO!", "Whoa, chill out!");
}
#[test]
#[ignore]
fn test_no_letters() {
process_response_case("1, 2, 3", "Whatever.");
}
#[test]
#[ignore]
fn test_question_with_no_letters() {
process_response_case("4?", "Sure.");
}
#[test]
#[ignore]
fn test_shouting_with_special_characters() {
process_response_case(
"ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!",
"Whoa, chill out!",
);
}
#[test]
#[ignore]
fn test_shouting_with_no_exclamation_mark() {
process_response_case("I HATE THE DMV", "Whoa, chill out!");
}
#[test]
#[ignore]
fn test_statement_containing_question_mark() {
process_response_case("Ending with ? means a question.", "Whatever.");
}
#[test]
#[ignore]
fn test_nonletters_with_question() {
process_response_case(":) ?", "Sure.");
}
#[test]
#[ignore]
fn test_prattling_on() {
process_response_case("Wait! Hang on. Are you going to be OK?", "Sure.");
}
#[test]
#[ignore]
fn test_silence() {
process_response_case("", "Fine. Be that way!");
}
#[test]
#[ignore]
fn test_prolonged_silence() {
process_response_case(" ", "Fine. Be that way!");
}
#[test]
#[ignore]
fn test_alternate_silence() {
process_response_case("\t\t\t\t\t\t\t\t\t\t", "Fine. Be that way!");
}
#[test]
#[ignore]
fn test_multiple_line_question() {
process_response_case(
"\nDoes this cryogenic chamber make me look fat?\nNo.",
"Whatever.",
);
}
#[test]
#[ignore]
fn test_starting_with_whitespace() {
process_response_case(" hmmmmmmm...", "Whatever.");
}
#[test]
#[ignore]
fn test_ending_with_whitespace() {
process_response_case("Okay if like my spacebar quite a bit? ", "Sure.");
}
#[test]
#[ignore]
fn test_other_whitespace() {
process_response_case("\n\r \t", "Fine. Be that way!");
}
#[test]
#[ignore]
fn test_nonquestion_ending_with_whitespace() {
process_response_case(
"This is a statement ending with whitespace ",
"Whatever.",
);
}
pub fn reply(message: &str) -> &str {
enum Quality {
Empty,
YellQuestion,
YellStatement,
AskQuestion,
Statement,
};
trait HasQuality {
fn has_quality(&self) -> Quality;
}
impl HasQuality for str {
fn has_quality(&self) -> Quality {
let message = self.trim();
let question = message.ends_with('?');
let any_alpha = message.chars().any(char::is_alphabetic);
let is_yelling = message.to_ascii_uppercase() == message;
let is_empty = message.len() == 0;
match (is_empty, any_alpha && is_yelling, question) {
(true, _, _) => Quality::Empty,
(false, true, true) => Quality::YellQuestion,
(false, true, false) => Quality::YellStatement,
(false, false, true) => Quality::AskQuestion,
_ => Quality::Statement,
}
}
};
match message.has_quality() {
Quality::Empty => "Fine. Be that way!",
Quality::YellQuestion => "Calm down, I know what I'm doing!",
Quality::YellStatement => "Whoa, chill out!",
Quality::AskQuestion => "Sure.",
_ => "Whatever.",
}
}
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments