From a675ae7b815fc3623f1e8231df60bd9eea5b2cb5 Mon Sep 17 00:00:00 2001 From: Avery Winters Date: Wed, 11 Oct 2023 13:32:05 -0500 Subject: [PATCH] Add rust/anagram --- rust/anagram/.exercism/config.json | 47 +++++++ rust/anagram/.exercism/metadata.json | 1 + rust/anagram/.gitignore | 8 ++ rust/anagram/Cargo.toml | 4 + rust/anagram/HELP.md | 86 +++++++++++++ rust/anagram/README.md | 61 +++++++++ rust/anagram/src/lib.rs | 5 + rust/anagram/tests/anagram.rs | 186 +++++++++++++++++++++++++++ 8 files changed, 398 insertions(+) create mode 100644 rust/anagram/.exercism/config.json create mode 100644 rust/anagram/.exercism/metadata.json create mode 100644 rust/anagram/.gitignore create mode 100644 rust/anagram/Cargo.toml create mode 100644 rust/anagram/HELP.md create mode 100644 rust/anagram/README.md create mode 100644 rust/anagram/src/lib.rs create mode 100644 rust/anagram/tests/anagram.rs diff --git a/rust/anagram/.exercism/config.json b/rust/anagram/.exercism/config.json new file mode 100644 index 0000000..b23f186 --- /dev/null +++ b/rust/anagram/.exercism/config.json @@ -0,0 +1,47 @@ +{ + "authors": [ + "EduardoBautista" + ], + "contributors": [ + "andrewclarkson", + "ashleygwilliams", + "bobahop", + "chancancode", + "ClashTheBunny", + "coriolinus", + "cwhakes", + "Dimkar3000", + "EduardoBautista", + "efx", + "ErikSchierboom", + "gris", + "IanWhitney", + "kytrinyx", + "lutostag", + "mkantor", + "nfiles", + "petertseng", + "pminten", + "quartsize", + "rofrol", + "stevejb71", + "stringparser", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs", + "Cargo.toml" + ], + "test": [ + "tests/anagram.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "blurb": "Given a word and a list of possible anagrams, select the correct sublist.", + "source": "Inspired by the Extreme Startup game", + "source_url": "https://github.com/rchatley/extreme_startup" +} diff --git a/rust/anagram/.exercism/metadata.json b/rust/anagram/.exercism/metadata.json new file mode 100644 index 0000000..8756648 --- /dev/null +++ b/rust/anagram/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"anagram","id":"5c5e6c9694c543239876c8e16341b39d","url":"https://exercism.org/tracks/rust/exercises/anagram","handle":"averywinters","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/anagram/.gitignore b/rust/anagram/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/anagram/.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/rust/anagram/Cargo.toml b/rust/anagram/Cargo.toml new file mode 100644 index 0000000..5e06e4e --- /dev/null +++ b/rust/anagram/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2021" +name = "anagram" +version = "0.0.0" diff --git a/rust/anagram/HELP.md b/rust/anagram/HELP.md new file mode 100644 index 0000000..67add76 --- /dev/null +++ b/rust/anagram/HELP.md @@ -0,0 +1,86 @@ +# 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) +- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- 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/rust/anagram/README.md b/rust/anagram/README.md new file mode 100644 index 0000000..e6be820 --- /dev/null +++ b/rust/anagram/README.md @@ -0,0 +1,61 @@ +# Anagram + +Welcome to Anagram on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +An anagram is a rearrangement of letters to form a new word. +Given a word and a list of candidates, select the sublist of anagrams of the given word. + +Given `"listen"` and a list of candidates like `"enlists" "google" +"inlets" "banana"` the program should return a list containing +`"inlets"`. + +The solution is case insensitive, which means `"WOrd"` is the same as `"word"` or `"woRd"`. It may help to take a peek at the [std library](https://doc.rust-lang.org/std/primitive.char.html) for functions that can convert between them. + +The solution cannot contain the input word. A word is always an anagram of itself, which means it is not an interesting result. Given `"hello"` and the list `["hello", "olleh"]` the answer is `["olleh"]`. + +You are going to have to adjust the function signature provided in the stub in order for the lifetimes to work out properly. This is intentional: what's there demonstrates the basics of lifetime syntax, and what's missing teaches how to interpret lifetime-related compiler errors. + +Try to limit case changes. Case changes are expensive in terms of time, so it's faster to minimize them. + +If sorting, consider [sort_unstable](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable) which is typically faster than stable sorting. When applicable, unstable sorting is preferred because it is generally faster than stable sorting and it doesn't allocate auxiliary memory. + +## Source + +### Created by + +- @EduardoBautista + +### Contributed to by + +- @andrewclarkson +- @ashleygwilliams +- @bobahop +- @chancancode +- @ClashTheBunny +- @coriolinus +- @cwhakes +- @Dimkar3000 +- @EduardoBautista +- @efx +- @ErikSchierboom +- @gris +- @IanWhitney +- @kytrinyx +- @lutostag +- @mkantor +- @nfiles +- @petertseng +- @pminten +- @quartsize +- @rofrol +- @stevejb71 +- @stringparser +- @xakon +- @ZapAnton + +### Based on + +Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup \ No newline at end of file diff --git a/rust/anagram/src/lib.rs b/rust/anagram/src/lib.rs new file mode 100644 index 0000000..f029d04 --- /dev/null +++ b/rust/anagram/src/lib.rs @@ -0,0 +1,5 @@ +use std::collections::HashSet; + +pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&str]) -> HashSet<&'a str> { + todo!("For the '{word}' word find anagrams among the following words: {possible_anagrams:?}"); +} diff --git a/rust/anagram/tests/anagram.rs b/rust/anagram/tests/anagram.rs new file mode 100644 index 0000000..4ac7a28 --- /dev/null +++ b/rust/anagram/tests/anagram.rs @@ -0,0 +1,186 @@ +use std::collections::HashSet; + +fn process_anagram_case(word: &str, inputs: &[&str], expected: &[&str]) { + let result = anagram::anagrams_for(word, inputs); + + let expected: HashSet<&str> = expected.iter().cloned().collect(); + + assert_eq!(result, expected); +} + +#[test] +fn no_matches() { + let word = "diaper"; + + let inputs = ["hello", "world", "zombies", "pants"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn detect_simple_anagram() { + let word = "ant"; + + let inputs = ["tan", "stand", "at"]; + + let outputs = vec!["tan"]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn does_not_confuse_different_duplicates() { + let word = "galea"; + + let inputs = ["eagle"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn eliminate_anagram_subsets() { + let word = "good"; + + let inputs = ["dog", "goody"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn detect_anagram() { + let word = "listen"; + + let inputs = ["enlists", "google", "inlets", "banana"]; + + let outputs = vec!["inlets"]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn multiple_anagrams() { + let word = "allergy"; + + let inputs = [ + "gallery", + "ballerina", + "regally", + "clergy", + "largely", + "leading", + ]; + + let outputs = vec!["gallery", "regally", "largely"]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn case_insensitive_anagrams() { + let word = "Orchestra"; + + let inputs = ["cashregister", "Carthorse", "radishes"]; + + let outputs = vec!["Carthorse"]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn unicode_anagrams() { + let word = "ΑΒΓ"; + + // These words don't make sense, they're just greek letters cobbled together. + let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"]; + + let outputs = vec!["ΒΓΑ", "γβα"]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn misleading_unicode_anagrams() { + // Despite what a human might think these words contain different letters, the input uses Greek + // A and B while the list of potential anagrams uses Latin A and B. + let word = "ΑΒΓ"; + + let inputs = ["ABΓ"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn does_not_detect_a_word_as_its_own_anagram() { + let word = "banana"; + + let inputs = ["banana"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn does_not_detect_a_differently_cased_word_as_its_own_anagram() { + let word = "banana"; + + let inputs = ["bAnana"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn does_not_detect_a_differently_cased_unicode_word_as_its_own_anagram() { + let word = "ΑΒΓ"; + + let inputs = ["ΑΒγ"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn same_bytes_different_chars() { + let word = "a⬂"; // 61 E2 AC 82 + + let inputs = ["€a"]; // E2 82 AC 61 + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +} + +#[test] +#[ignore] +fn different_words_but_same_ascii_sum() { + let word = "bc"; + + let inputs = ["ad"]; + + let outputs = vec![]; + + process_anagram_case(word, &inputs, &outputs); +}