From ce5151f61c6cd3fbee158f7c94caed7d7e6eed8a Mon Sep 17 00:00:00 2001 From: Gary Yendell Date: Sat, 22 Oct 2022 23:10:41 +0100 Subject: [PATCH] Exercism 8 - Would be nice to be able to get enum value from int... --- Cargo.toml | 1 + allergies/.exercism/config.json | 40 +++++++++ allergies/.exercism/metadata.json | 1 + allergies/.gitignore | 8 ++ allergies/Cargo.toml | 4 + allergies/HELP.md | 85 ++++++++++++++++++ allergies/README.md | 66 ++++++++++++++ allergies/src/lib.rs | 51 +++++++++++ allergies/tests/allergies.rs | 143 ++++++++++++++++++++++++++++++ 9 files changed, 399 insertions(+) create mode 100644 allergies/.exercism/config.json create mode 100644 allergies/.exercism/metadata.json create mode 100644 allergies/.gitignore create mode 100644 allergies/Cargo.toml create mode 100644 allergies/HELP.md create mode 100644 allergies/README.md create mode 100644 allergies/src/lib.rs create mode 100644 allergies/tests/allergies.rs diff --git a/Cargo.toml b/Cargo.toml index 97c5616..acda14d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ members = [ "assembly-line", "health-statistics", "clock", + "allergies", ] diff --git a/allergies/.exercism/config.json b/allergies/.exercism/config.json new file mode 100644 index 0000000..7b803c8 --- /dev/null +++ b/allergies/.exercism/config.json @@ -0,0 +1,40 @@ +{ + "blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.", + "authors": [ + "EduardoBautista" + ], + "contributors": [ + "ashleygwilliams", + "coriolinus", + "cwhakes", + "EduardoBautista", + "efx", + "ErikSchierboom", + "IanWhitney", + "kytrinyx", + "lutostag", + "mkantor", + "navossoc", + "nfiles", + "petertseng", + "rofrol", + "stringparser", + "taravancil", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs", + "Cargo.toml" + ], + "test": [ + "tests/allergies.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "source": "Jumpstart Lab Warm-up", + "source_url": "http://jumpstartlab.com" +} diff --git a/allergies/.exercism/metadata.json b/allergies/.exercism/metadata.json new file mode 100644 index 0000000..9e885d1 --- /dev/null +++ b/allergies/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"allergies","id":"9ef20766f1cd4a89a746c322d53be341","url":"https://exercism.org/tracks/rust/exercises/allergies","handle":"GDYendell","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/allergies/.gitignore b/allergies/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/allergies/.gitignore @@ -0,0 +1,8 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +**/*.rs.bk + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/allergies/Cargo.toml b/allergies/Cargo.toml new file mode 100644 index 0000000..7bd71fc --- /dev/null +++ b/allergies/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2021" +name = "allergies" +version = "1.1.0" diff --git a/allergies/HELP.md b/allergies/HELP.md new file mode 100644 index 0000000..40d8a33 --- /dev/null +++ b/allergies/HELP.md @@ -0,0 +1,85 @@ +# Help + +## Running the tests + +Execute the tests with: + +```bash +$ 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 _only ignored_ tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +If you are using Rust 1.51 or later, you can run _all_ tests with + +```bash +$ cargo test -- --include-ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored, use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the online [test documentation][rust-tests]. + +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Rust track's documentation](https://exercism.org/docs/tracks/rust) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Submitting the solution + +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. + +## Feedback, Issues, Pull Requests + +The GitHub [track repository][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]. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + +[help-page]: https://exercism.org/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.org/docs/community/contributors \ No newline at end of file diff --git a/allergies/README.md b/allergies/README.md new file mode 100644 index 0000000..e4232fd --- /dev/null +++ b/allergies/README.md @@ -0,0 +1,66 @@ +# Allergies + +Welcome to Allergies on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies. + +An allergy test produces a single numeric score which contains the +information about all the allergies the person has (that they were +tested for). + +The list of items (and their value) that were tested are: + +* eggs (1) +* peanuts (2) +* shellfish (4) +* strawberries (8) +* tomatoes (16) +* chocolate (32) +* pollen (64) +* cats (128) + +So if Tom is allergic to peanuts and chocolate, he gets a score of 34. + +Now, given just that score of 34, your program should be able to say: + +- Whether Tom is allergic to any one of those allergens listed above. +- All the allergens Tom is allergic to. + +Note: a given score may include allergens **not** listed above (i.e. +allergens that score 256, 512, 1024, etc.). Your program should +ignore those components of the score. For example, if the allergy +score is 257, your program should only report the eggs (1) allergy. + +## Source + +### Created by + +- @EduardoBautista + +### Contributed to by + +- @ashleygwilliams +- @coriolinus +- @cwhakes +- @EduardoBautista +- @efx +- @ErikSchierboom +- @IanWhitney +- @kytrinyx +- @lutostag +- @mkantor +- @navossoc +- @nfiles +- @petertseng +- @rofrol +- @stringparser +- @taravancil +- @xakon +- @ZapAnton + +### Based on + +Jumpstart Lab Warm-up - http://jumpstartlab.com \ No newline at end of file diff --git a/allergies/src/lib.rs b/allergies/src/lib.rs new file mode 100644 index 0000000..cc474df --- /dev/null +++ b/allergies/src/lib.rs @@ -0,0 +1,51 @@ +pub struct Allergies { + score: u32, +} + +#[derive(Debug, PartialEq, Eq)] +pub enum Allergen { + Eggs, + Peanuts, + Shellfish, + Strawberries, + Tomatoes, + Chocolate, + Pollen, + Cats, +} + +fn allergen_by_index(index: u32) -> Allergen { + use Allergen::*; + match index { + 0 => Eggs, + 1 => Peanuts, + 2 => Shellfish, + 3 => Strawberries, + 4 => Tomatoes, + 5 => Chocolate, + 6 => Pollen, + 7 => Cats, + _ => panic!("Invalid index"), + } +} + +impl Allergies { + pub fn new(score: u32) -> Self { + Allergies { score } + } + + pub fn is_allergic_to(&self, allergen: &Allergen) -> bool { + self.score_bit_set(*allergen as u32) + } + + pub fn allergies(&self) -> Vec { + (0..=7) + .filter(|idx| self.score_bit_set(*idx)) + .map(allergen_by_index) + .collect() + } + + fn score_bit_set(&self, bit: u32) -> bool { + (self.score >> bit) & 1 == 1 + } +} diff --git a/allergies/tests/allergies.rs b/allergies/tests/allergies.rs new file mode 100644 index 0000000..e502ae3 --- /dev/null +++ b/allergies/tests/allergies.rs @@ -0,0 +1,143 @@ +use allergies::*; + +fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) { + for element in expected { + if !actual.contains(element) { + panic!( + "Allergen missing\n {:?} should be in {:?}", + element, actual + ); + } + } + + if actual.len() != expected.len() { + panic!( + "Allergy vectors are of different lengths\n expected {:?}\n got {:?}", + expected, actual + ); + } +} + +#[test] +fn is_not_allergic_to_anything() { + let allergies = Allergies::new(0); + assert!(!allergies.is_allergic_to(&Allergen::Peanuts)); + assert!(!allergies.is_allergic_to(&Allergen::Cats)); + assert!(!allergies.is_allergic_to(&Allergen::Strawberries)); +} + +#[test] +fn is_allergic_to_eggs() { + assert!(Allergies::new(1).is_allergic_to(&Allergen::Eggs)); +} + +#[test] +fn is_allergic_to_eggs_and_shellfish_but_not_strawberries() { + let allergies = Allergies::new(5); + assert!(allergies.is_allergic_to(&Allergen::Eggs)); + assert!(allergies.is_allergic_to(&Allergen::Shellfish)); + assert!(!allergies.is_allergic_to(&Allergen::Strawberries)); +} + +#[test] +fn no_allergies_at_all() { + let expected = &[]; + let allergies = Allergies::new(0).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_just_eggs() { + let expected = &[Allergen::Eggs]; + let allergies = Allergies::new(1).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_just_peanuts() { + let expected = &[Allergen::Peanuts]; + let allergies = Allergies::new(2).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_just_strawberries() { + let expected = &[Allergen::Strawberries]; + let allergies = Allergies::new(8).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_just_cats() { + let expected = &[Allergen::Cats]; + let allergies = Allergies::new(128).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_eggs_and_peanuts() { + let expected = &[Allergen::Eggs, Allergen::Peanuts]; + let allergies = Allergies::new(3).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_eggs_and_shellfish() { + let expected = &[Allergen::Eggs, Allergen::Shellfish]; + let allergies = Allergies::new(5).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_many_things() { + let expected = &[ + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(248).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn allergic_to_everything() { + let expected = &[ + Allergen::Eggs, + Allergen::Peanuts, + Allergen::Shellfish, + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(255).allergies(); + + compare_allergy_vectors(expected, &allergies); +} + +#[test] +fn scores_over_255_do_not_trigger_false_positives() { + let expected = &[ + Allergen::Eggs, + Allergen::Shellfish, + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(509).allergies(); + + compare_allergy_vectors(expected, &allergies); +}