diff options
| author | Federico Igne <git@federicoigne.com> | 2020-07-13 08:06:08 +0200 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | 354a0ed1725ae5408e097164714599b6b1bb66c8 (patch) | |
| tree | ed907fe660c2c7fae9521bdcb99658b1f3165f1c /bob | |
| parent | 64cc39e6507a611366e1c3ddcf098b77c94a38db (diff) | |
| download | exercism-354a0ed1725ae5408e097164714599b6b1bb66c8.tar.gz exercism-354a0ed1725ae5408e097164714599b6b1bb66c8.zip | |
[rust] Bob
Diffstat (limited to 'bob')
| -rw-r--r-- | bob/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | bob/.gitignore | 8 | ||||
| -rw-r--r-- | bob/Cargo.toml | 4 | ||||
| -rw-r--r-- | bob/README.md | 96 | ||||
| -rw-r--r-- | bob/src/lib.rs | 75 | ||||
| -rw-r--r-- | bob/tests/bob.rs | 179 |
6 files changed, 363 insertions, 0 deletions
diff --git a/bob/.exercism/metadata.json b/bob/.exercism/metadata.json new file mode 100644 index 0000000..c9dfddb --- /dev/null +++ b/bob/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"bob","id":"4e53ee4ebf414ac3b9da60e7011c04f9","url":"https://exercism.io/my/solutions/4e53ee4ebf414ac3b9da60e7011c04f9","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/bob/.gitignore b/bob/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/bob/.gitignore | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/bob/Cargo.toml b/bob/Cargo.toml new file mode 100644 index 0000000..34a7fbf --- /dev/null +++ b/bob/Cargo.toml | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "bob" | ||
| 4 | version = "1.6.0" | ||
diff --git a/bob/README.md b/bob/README.md new file mode 100644 index 0000000..dd84948 --- /dev/null +++ b/bob/README.md | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | # Bob | ||
| 2 | |||
| 3 | Bob is a lackadaisical teenager. In conversation, his responses are very limited. | ||
| 4 | |||
| 5 | Bob answers 'Sure.' if you ask him a question, such as "How are you?". | ||
| 6 | |||
| 7 | He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals). | ||
| 8 | |||
| 9 | He answers 'Calm down, I know what I'm doing!' if you yell a question at him. | ||
| 10 | |||
| 11 | He says 'Fine. Be that way!' if you address him without actually saying | ||
| 12 | anything. | ||
| 13 | |||
| 14 | He answers 'Whatever.' to anything else. | ||
| 15 | |||
| 16 | Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English. | ||
| 17 | |||
| 18 | ## Rust Installation | ||
| 19 | |||
| 20 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 21 | resources. | ||
| 22 | |||
| 23 | ## Writing the Code | ||
| 24 | |||
| 25 | Execute the tests with: | ||
| 26 | |||
| 27 | ```bash | ||
| 28 | $ cargo test | ||
| 29 | ``` | ||
| 30 | |||
| 31 | All but the first test have been ignored. After you get the first test to | ||
| 32 | pass, open the tests source file which is located in the `tests` directory | ||
| 33 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 34 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 35 | Continue, until you pass every test. | ||
| 36 | |||
| 37 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 38 | |||
| 39 | ```bash | ||
| 40 | $ cargo test -- --ignored | ||
| 41 | ``` | ||
| 42 | |||
| 43 | To run a specific test, for example `some_test`, you can use: | ||
| 44 | |||
| 45 | ```bash | ||
| 46 | $ cargo test some_test | ||
| 47 | ``` | ||
| 48 | |||
| 49 | If the specific test is ignored use: | ||
| 50 | |||
| 51 | ```bash | ||
| 52 | $ cargo test some_test -- --ignored | ||
| 53 | ``` | ||
| 54 | |||
| 55 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 56 | |||
| 57 | Make sure to read the [Modules][modules] chapter if you | ||
| 58 | haven't already, it will help you with organizing your files. | ||
| 59 | |||
| 60 | ## Further improvements | ||
| 61 | |||
| 62 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 63 | |||
| 64 | To format your solution, inside the solution directory use | ||
| 65 | |||
| 66 | ```bash | ||
| 67 | cargo fmt | ||
| 68 | ``` | ||
| 69 | |||
| 70 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 71 | |||
| 72 | ```bash | ||
| 73 | cargo clippy --all-targets | ||
| 74 | ``` | ||
| 75 | |||
| 76 | ## Submitting the solution | ||
| 77 | |||
| 78 | 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. | ||
| 79 | |||
| 80 | ## Feedback, Issues, Pull Requests | ||
| 81 | |||
| 82 | The [exercism/rust](https://github.com/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! | ||
| 83 | |||
| 84 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 85 | |||
| 86 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 87 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 88 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 89 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 90 | |||
| 91 | ## Source | ||
| 92 | |||
| 93 | Inspired by the 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06) | ||
| 94 | |||
| 95 | ## Submitting Incomplete Solutions | ||
| 96 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/bob/src/lib.rs b/bob/src/lib.rs new file mode 100644 index 0000000..ae18a3d --- /dev/null +++ b/bob/src/lib.rs | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | /* The solution consists of an implementation of a simple Finite State Automaton whose states and | ||
| 2 | * transition messages are represented by the `State` and `Message` enums. | ||
| 3 | * | ||
| 4 | * `process(State, Message) -> State` represents the main transition function for the FSA. This | ||
| 5 | * allows us to compute the answer with a single visit of the input string. | ||
| 6 | * | ||
| 7 | * Note: some duplicate states (`QuestLow`, `ElseLow`) are needed in order to remember whether we | ||
| 8 | * have seen a lowercase character already or not. This is probably not the minimal number of | ||
| 9 | * states required to solve the problem. | ||
| 10 | */ | ||
| 11 | enum State { Nothing, QuestLow, Quest, Yell, YellQ, ElseLow, Else } | ||
| 12 | enum Message { Whitespace, Upper, Lower, Symbol, Question } | ||
| 13 | |||
| 14 | fn to_message(c: char) -> Message { | ||
| 15 | match c as u8 { | ||
| 16 | 0..=32 => Message::Whitespace, // All controll characters are considered whitespace | ||
| 17 | 63 => Message::Question, | ||
| 18 | 65..=90 => Message::Upper, | ||
| 19 | 97..=122 => Message::Lower, | ||
| 20 | _ => Message::Symbol // Anything else (numbers incl.) is a symbol | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | fn process(s: State, m: Message) -> State { | ||
| 25 | match s { | ||
| 26 | State::Nothing => match m { | ||
| 27 | Message::Whitespace => s, | ||
| 28 | Message::Upper => State::Yell, | ||
| 29 | Message::Lower => State::ElseLow, | ||
| 30 | Message::Question => State::Quest, | ||
| 31 | Message::Symbol => State::Else | ||
| 32 | }, | ||
| 33 | State::QuestLow => match m { | ||
| 34 | Message::Whitespace | Message::Question => s, | ||
| 35 | _ => State::ElseLow | ||
| 36 | }, | ||
| 37 | State::Quest => match m { | ||
| 38 | Message::Upper => State::Yell, | ||
| 39 | Message::Lower => State::ElseLow, | ||
| 40 | Message::Symbol => State::Else, | ||
| 41 | _ => s | ||
| 42 | }, | ||
| 43 | State::Yell => match m { | ||
| 44 | Message::Lower => State::ElseLow, | ||
| 45 | Message::Question => State::YellQ, | ||
| 46 | _ => s | ||
| 47 | }, | ||
| 48 | State::YellQ => match m { | ||
| 49 | Message::Symbol | Message::Upper => State::Yell, | ||
| 50 | Message::Lower => State::ElseLow, | ||
| 51 | _ => s | ||
| 52 | }, | ||
| 53 | State::ElseLow => match m { | ||
| 54 | Message::Question => State::QuestLow, | ||
| 55 | _ => s | ||
| 56 | }, | ||
| 57 | State::Else => match m { | ||
| 58 | Message::Upper => State::Yell, | ||
| 59 | Message::Lower => State::ElseLow, | ||
| 60 | Message::Question => State::Quest, | ||
| 61 | _ => s | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | pub fn reply(message: &str) -> &str { | ||
| 67 | let state = message.chars().fold(State::Nothing, |s, c| process(s,to_message(c))); | ||
| 68 | match state { | ||
| 69 | State::Nothing => "Fine. Be that way!", | ||
| 70 | State::QuestLow | State::Quest => "Sure.", | ||
| 71 | State::Yell => "Whoa, chill out!", | ||
| 72 | State::YellQ => "Calm down, I know what I'm doing!", | ||
| 73 | _ => "Whatever." | ||
| 74 | } | ||
| 75 | } | ||
diff --git a/bob/tests/bob.rs b/bob/tests/bob.rs new file mode 100644 index 0000000..b6221eb --- /dev/null +++ b/bob/tests/bob.rs | |||
| @@ -0,0 +1,179 @@ | |||
| 1 | fn process_response_case(phrase: &str, expected_response: &str) { | ||
| 2 | assert_eq!(bob::reply(phrase), expected_response); | ||
| 3 | } | ||
| 4 | |||
| 5 | #[test] | ||
| 6 | /// stating something | ||
| 7 | fn test_stating_something() { | ||
| 8 | process_response_case("Tom-ay-to, tom-aaaah-to.", "Whatever."); | ||
| 9 | } | ||
| 10 | |||
| 11 | |||
| 12 | #[test] | ||
| 13 | /// ending with whitespace | ||
| 14 | fn test_ending_with_whitespace() { | ||
| 15 | process_response_case("Okay if like my spacebar quite a bit? ", "Sure."); | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | #[test] | ||
| 20 | /// shouting numbers | ||
| 21 | fn test_shouting_numbers() { | ||
| 22 | process_response_case("1, 2, 3 GO!", "Whoa, chill out!"); | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | #[test] | ||
| 27 | /// other whitespace | ||
| 28 | fn test_other_whitespace() { | ||
| 29 | process_response_case("\r\r ", "Fine. Be that way!"); | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | #[test] | ||
| 34 | /// shouting with special characters | ||
| 35 | fn test_shouting_with_special_characters() { | ||
| 36 | process_response_case("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!", "Whoa, chill out!"); | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | #[test] | ||
| 41 | /// talking forcefully | ||
| 42 | fn test_talking_forcefully() { | ||
| 43 | process_response_case("Hi there!", "Whatever."); | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | #[test] | ||
| 48 | /// prattling on | ||
| 49 | fn test_prattling_on() { | ||
| 50 | process_response_case("Wait! Hang on. Are you going to be OK?", "Sure."); | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | #[test] | ||
| 55 | /// forceful question | ||
| 56 | fn test_forceful_question() { | ||
| 57 | process_response_case("WHAT'S GOING ON?", "Calm down, I know what I'm doing!"); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | #[test] | ||
| 62 | /// shouting with no exclamation mark | ||
| 63 | fn test_shouting_with_no_exclamation_mark() { | ||
| 64 | process_response_case("I HATE THE DENTIST", "Whoa, chill out!"); | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | #[test] | ||
| 69 | /// asking gibberish | ||
| 70 | fn test_asking_gibberish() { | ||
| 71 | process_response_case("fffbbcbeab?", "Sure."); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | #[test] | ||
| 76 | /// question with no letters | ||
| 77 | fn test_question_with_no_letters() { | ||
| 78 | process_response_case("4?", "Sure."); | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | #[test] | ||
| 83 | /// no letters | ||
| 84 | fn test_no_letters() { | ||
| 85 | process_response_case("1, 2, 3", "Whatever."); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | #[test] | ||
| 90 | /// statement containing question mark | ||
| 91 | fn test_statement_containing_question_mark() { | ||
| 92 | process_response_case("Ending with ? means a question.", "Whatever."); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | //NEW | ||
| 97 | #[test] | ||
| 98 | /// multiple line question | ||
| 99 | fn test_multiple_line_question() { | ||
| 100 | process_response_case("\rDoes this cryogenic chamber make me look fat?\rNo.", "Whatever."); | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | #[test] | ||
| 105 | /// non-question ending with whitespace | ||
| 106 | fn test_nonquestion_ending_with_whitespace() { | ||
| 107 | process_response_case("This is a statement ending with whitespace ", "Whatever."); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | #[test] | ||
| 112 | /// shouting | ||
| 113 | fn test_shouting() { | ||
| 114 | process_response_case("WATCH OUT!", "Whoa, chill out!"); | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | #[test] | ||
| 119 | /// non-letters with question | ||
| 120 | fn test_nonletters_with_question() { | ||
| 121 | process_response_case(":) ?", "Sure."); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | #[test] | ||
| 126 | /// shouting gibberish | ||
| 127 | fn test_shouting_gibberish() { | ||
| 128 | process_response_case("FCECDFCAAB", "Whoa, chill out!"); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | #[test] | ||
| 133 | /// asking a question | ||
| 134 | fn test_asking_a_question() { | ||
| 135 | process_response_case("Does this cryogenic chamber make me look fat?", "Sure."); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | #[test] | ||
| 140 | /// asking a numeric question | ||
| 141 | fn test_asking_a_numeric_question() { | ||
| 142 | process_response_case("You are, what, like 15?", "Sure."); | ||
| 143 | } | ||
| 144 | |||
| 145 | |||
| 146 | #[test] | ||
| 147 | /// silence | ||
| 148 | fn test_silence() { | ||
| 149 | process_response_case("", "Fine. Be that way!"); | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | #[test] | ||
| 154 | /// starting with whitespace | ||
| 155 | fn test_starting_with_whitespace() { | ||
| 156 | process_response_case(" hmmmmmmm...", "Whatever."); | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | #[test] | ||
| 161 | /// using acronyms in regular speech | ||
| 162 | fn test_using_acronyms_in_regular_speech() { | ||
| 163 | process_response_case("It's OK if you don't want to go work for NASA.", "Whatever."); | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | #[test] | ||
| 168 | /// alternate silence | ||
| 169 | fn test_alternate_silence() { | ||
| 170 | process_response_case(" ", "Fine. Be that way!"); | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | #[test] | ||
| 175 | /// prolonged silence | ||
| 176 | fn test_prolonged_silence() { | ||
| 177 | process_response_case(" ", "Fine. Be that way!"); | ||
| 178 | } | ||
| 179 | |||
