Exercism track submissions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

anagram

+403
+46
rust/anagram/.exercism/config.json
··· 1 + { 2 + "blurb": "Given a word and a list of possible anagrams, select the correct sublist.", 3 + "authors": [ 4 + "EduardoBautista" 5 + ], 6 + "contributors": [ 7 + "andrewclarkson", 8 + "ashleygwilliams", 9 + "bobahop", 10 + "chancancode", 11 + "ClashTheBunny", 12 + "coriolinus", 13 + "cwhakes", 14 + "Dimkar3000", 15 + "EduardoBautista", 16 + "efx", 17 + "ErikSchierboom", 18 + "gris", 19 + "IanWhitney", 20 + "kytrinyx", 21 + "lutostag", 22 + "mkantor", 23 + "nfiles", 24 + "petertseng", 25 + "pminten", 26 + "quartsize", 27 + "rofrol", 28 + "stevejb71", 29 + "stringparser", 30 + "xakon", 31 + "ZapAnton" 32 + ], 33 + "files": { 34 + "solution": [ 35 + "src/lib.rs" 36 + ], 37 + "test": [ 38 + "tests/anagram.rs" 39 + ], 40 + "example": [ 41 + ".meta/example.rs" 42 + ] 43 + }, 44 + "source": "Inspired by the Extreme Startup game", 45 + "source_url": "https://github.com/rchatley/extreme_startup" 46 + }
+8
rust/anagram/.gitignore
··· 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
+4
rust/anagram/Cargo.toml
··· 1 + [package] 2 + edition = "2021" 3 + name = "anagram" 4 + version = "0.0.0"
+85
rust/anagram/HELP.md
··· 1 + # Help 2 + 3 + ## Running the tests 4 + 5 + Execute the tests with: 6 + 7 + ```bash 8 + $ cargo test 9 + ``` 10 + 11 + All but the first test have been ignored. After you get the first test to 12 + pass, open the tests source file which is located in the `tests` directory 13 + and remove the `#[ignore]` flag from the next test and get the tests to pass 14 + again. Each separate test is a function with `#[test]` flag above it. 15 + Continue, until you pass every test. 16 + 17 + If you wish to run _only ignored_ tests without editing the tests source file, use: 18 + 19 + ```bash 20 + $ cargo test -- --ignored 21 + ``` 22 + 23 + If you are using Rust 1.51 or later, you can run _all_ tests with 24 + 25 + ```bash 26 + $ cargo test -- --include-ignored 27 + ``` 28 + 29 + To run a specific test, for example `some_test`, you can use: 30 + 31 + ```bash 32 + $ cargo test some_test 33 + ``` 34 + 35 + If the specific test is ignored, use: 36 + 37 + ```bash 38 + $ cargo test some_test -- --ignored 39 + ``` 40 + 41 + To learn more about Rust tests refer to the online [test documentation][rust-tests]. 42 + 43 + [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html 44 + 45 + ## Submitting your solution 46 + 47 + You can submit your solution using the `exercism submit src/lib.rs` command. 48 + This command will upload your solution to the Exercism website and print the solution page's URL. 49 + 50 + It's possible to submit an incomplete solution which allows you to: 51 + 52 + - See how others have completed the exercise 53 + - Request help from a mentor 54 + 55 + ## Need to get help? 56 + 57 + If you'd like help solving the exercise, check the following pages: 58 + 59 + - The [Rust track's documentation](https://exercism.org/docs/tracks/rust) 60 + - [Exercism's support channel on gitter](https://gitter.im/exercism/support) 61 + - The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) 62 + 63 + Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. 64 + 65 + ## Rust Installation 66 + 67 + Refer to the [exercism help page][help-page] for Rust installation and learning 68 + resources. 69 + 70 + ## Submitting the solution 71 + 72 + 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. 73 + 74 + ## Feedback, Issues, Pull Requests 75 + 76 + 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! 77 + 78 + If you want to know more about Exercism, take a look at the [contribution guide]. 79 + 80 + ## Submitting Incomplete Solutions 81 + It's possible to submit an incomplete solution so you can see how others have completed the exercise. 82 + 83 + [help-page]: https://exercism.io/tracks/rust/learning 84 + [github]: https://github.com/exercism/rust 85 + [contribution guide]: https://exercism.io/docs/community/contributors
+61
rust/anagram/README.md
··· 1 + # Anagram 2 + 3 + Welcome to Anagram on Exercism's Rust Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Instructions 7 + 8 + An anagram is a rearrangement of letters to form a new word. 9 + Given a word and a list of candidates, select the sublist of anagrams of the given word. 10 + 11 + Given `"listen"` and a list of candidates like `"enlists" "google" 12 + "inlets" "banana"` the program should return a list containing 13 + `"inlets"`. 14 + 15 + 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. 16 + 17 + 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"]`. 18 + 19 + 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. 20 + 21 + Try to limit case changes. Case changes are expensive in terms of time, so it's faster to minimize them. 22 + 23 + 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. 24 + 25 + ## Source 26 + 27 + ### Created by 28 + 29 + - @EduardoBautista 30 + 31 + ### Contributed to by 32 + 33 + - @andrewclarkson 34 + - @ashleygwilliams 35 + - @bobahop 36 + - @chancancode 37 + - @ClashTheBunny 38 + - @coriolinus 39 + - @cwhakes 40 + - @Dimkar3000 41 + - @EduardoBautista 42 + - @efx 43 + - @ErikSchierboom 44 + - @gris 45 + - @IanWhitney 46 + - @kytrinyx 47 + - @lutostag 48 + - @mkantor 49 + - @nfiles 50 + - @petertseng 51 + - @pminten 52 + - @quartsize 53 + - @rofrol 54 + - @stevejb71 55 + - @stringparser 56 + - @xakon 57 + - @ZapAnton 58 + 59 + ### Based on 60 + 61 + Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup
+15
rust/anagram/src/lib.rs
··· 1 + use std::collections::HashSet; 2 + 3 + fn hash(x: &str) -> String { 4 + let mut y = x.to_lowercase().chars().collect::<Vec<char>>(); 5 + y.sort_unstable(); 6 + y.iter().collect() 7 + } 8 + pub fn anagrams_for<'a, 'b>(word: &'a str, possible_anagrams: &'b [&'a str]) -> HashSet<&'a str> { 9 + let w_hash = hash(word); 10 + possible_anagrams 11 + .iter() 12 + .filter(|x| x.to_lowercase() != word.to_lowercase() && w_hash == hash(x)) 13 + .cloned() 14 + .collect() 15 + }
+184
rust/anagram/tests/anagram.rs
··· 1 + use std::collections::HashSet; 2 + 3 + fn process_anagram_case(word: &str, inputs: &[&str], expected: &[&str]) { 4 + let result = anagram::anagrams_for(word, inputs); 5 + 6 + let expected: HashSet<&str> = expected.iter().cloned().collect(); 7 + 8 + assert_eq!(result, expected); 9 + } 10 + 11 + #[test] 12 + fn test_no_matches() { 13 + let word = "diaper"; 14 + 15 + let inputs = ["hello", "world", "zombies", "pants"]; 16 + 17 + let outputs = vec![]; 18 + 19 + process_anagram_case(word, &inputs, &outputs); 20 + } 21 + 22 + #[test] 23 + #[ignore] 24 + fn test_detect_simple_anagram() { 25 + let word = "ant"; 26 + 27 + let inputs = ["tan", "stand", "at"]; 28 + 29 + let outputs = vec!["tan"]; 30 + 31 + process_anagram_case(word, &inputs, &outputs); 32 + } 33 + 34 + #[test] 35 + #[ignore] 36 + fn test_does_not_confuse_different_duplicates() { 37 + let word = "galea"; 38 + 39 + let inputs = ["eagle"]; 40 + 41 + let outputs = vec![]; 42 + 43 + process_anagram_case(word, &inputs, &outputs); 44 + } 45 + 46 + #[test] 47 + #[ignore] 48 + fn test_eliminate_anagram_subsets() { 49 + let word = "good"; 50 + 51 + let inputs = ["dog", "goody"]; 52 + 53 + let outputs = vec![]; 54 + 55 + process_anagram_case(word, &inputs, &outputs); 56 + } 57 + 58 + #[test] 59 + #[ignore] 60 + fn test_detect_anagram() { 61 + let word = "listen"; 62 + 63 + let inputs = ["enlists", "google", "inlets", "banana"]; 64 + 65 + let outputs = vec!["inlets"]; 66 + 67 + process_anagram_case(word, &inputs, &outputs); 68 + } 69 + 70 + #[test] 71 + #[ignore] 72 + fn test_multiple_anagrams() { 73 + let word = "allergy"; 74 + 75 + let inputs = [ 76 + "gallery", 77 + "ballerina", 78 + "regally", 79 + "clergy", 80 + "largely", 81 + "leading", 82 + ]; 83 + 84 + let outputs = vec!["gallery", "regally", "largely"]; 85 + 86 + process_anagram_case(word, &inputs, &outputs); 87 + } 88 + 89 + #[test] 90 + #[ignore] 91 + fn test_case_insensitive_anagrams() { 92 + let word = "Orchestra"; 93 + 94 + let inputs = ["cashregister", "Carthorse", "radishes"]; 95 + 96 + let outputs = vec!["Carthorse"]; 97 + 98 + process_anagram_case(word, &inputs, &outputs); 99 + } 100 + 101 + #[test] 102 + #[ignore] 103 + fn test_unicode_anagrams() { 104 + let word = "ΑΒΓ"; 105 + 106 + // These words don't make sense, they're just greek letters cobbled together. 107 + let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"]; 108 + 109 + let outputs = vec!["ΒΓΑ", "γβα"]; 110 + 111 + process_anagram_case(word, &inputs, &outputs); 112 + } 113 + 114 + #[test] 115 + #[ignore] 116 + fn test_misleading_unicode_anagrams() { 117 + // Despite what a human might think these words contain different letters, the input uses Greek 118 + // A and B while the list of potential anagrams uses Latin A and B. 119 + let word = "ΑΒΓ"; 120 + 121 + let inputs = ["ABΓ"]; 122 + 123 + let outputs = vec![]; 124 + 125 + process_anagram_case(word, &inputs, &outputs); 126 + } 127 + 128 + #[test] 129 + fn test_does_not_detect_a_word_as_its_own_anagram() { 130 + let word = "banana"; 131 + 132 + let inputs = ["banana"]; 133 + 134 + let outputs = vec![]; 135 + 136 + process_anagram_case(word, &inputs, &outputs); 137 + } 138 + 139 + #[test] 140 + fn test_does_not_detect_a_differently_cased_word_as_its_own_anagram() { 141 + let word = "banana"; 142 + 143 + let inputs = ["bAnana"]; 144 + 145 + let outputs = vec![]; 146 + 147 + process_anagram_case(word, &inputs, &outputs); 148 + } 149 + 150 + #[test] 151 + #[ignore] 152 + fn test_does_not_detect_a_differently_cased_unicode_word_as_its_own_anagram() { 153 + let word = "ΑΒΓ"; 154 + 155 + let inputs = ["ΑΒγ"]; 156 + 157 + let outputs = vec![]; 158 + 159 + process_anagram_case(word, &inputs, &outputs); 160 + } 161 + 162 + #[test] 163 + #[ignore] 164 + fn test_same_bytes_different_chars() { 165 + let word = "a⬂"; // 61 E2 AC 82 166 + 167 + let inputs = ["€a"]; // E2 82 AC 61 168 + 169 + let outputs = vec![]; 170 + 171 + process_anagram_case(word, &inputs, &outputs); 172 + } 173 + 174 + #[test] 175 + #[ignore] 176 + fn test_different_words_but_same_ascii_sum() { 177 + let word = "bc"; 178 + 179 + let inputs = ["ad"]; 180 + 181 + let outputs = vec![]; 182 + 183 + process_anagram_case(word, &inputs, &outputs); 184 + }