10: Add solution to Rust track exercise "Anagram".

[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Oct 11, 2021, 2:11 PM
RLDSDI3CZTZJRUN2DYOPB4JZ4YPHVV3NTJ5EMIMLRBAWTSSJT55AC

Dependencies

  • [2] CYRUJI4W 9: Add Rust track exercise "Anagram".

Change contents

  • replacement in rust/anagram/tests/anagram.rs at line 4
    [2.180][2.180:234]()
    let result = anagram::anagrams_for(word, inputs);
    [2.180]
    [2.234]
    let result = anagram::anagrams_for(word, inputs);
  • replacement in rust/anagram/tests/anagram.rs at line 6
    [2.235][2.235:305]()
    let expected: HashSet<&str> = expected.iter().cloned().collect();
    [2.235]
    [2.305]
    let expected: HashSet<&str> = expected.iter().cloned().collect();
  • replacement in rust/anagram/tests/anagram.rs at line 8
    [2.306][2.306:340]()
    assert_eq!(result, expected);
    [2.306]
    [2.340]
    assert_eq!(result, expected);
  • replacement in rust/anagram/tests/anagram.rs at line 13
    [2.374][2.374:399]()
    let word = "diaper";
    [2.374]
    [2.399]
    let word = "diaper";
  • replacement in rust/anagram/tests/anagram.rs at line 15
    [2.400][2.400:457]()
    let inputs = ["hello", "world", "zombies", "pants"];
    [2.400]
    [2.457]
    let inputs = ["hello", "world", "zombies", "pants"];
  • replacement in rust/anagram/tests/anagram.rs at line 17
    [2.458][2.458:484]()
    let outputs = vec![];
    [2.458]
    [2.484]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 19
    [2.485][2.485:536]()
    process_anagram_case(word, &inputs, &outputs);
    [2.485]
    [2.536]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 25
    [2.591][2.591:613]()
    let word = "ant";
    [2.591]
    [2.613]
    let word = "ant";
  • replacement in rust/anagram/tests/anagram.rs at line 27
    [2.614][2.614:655]()
    let inputs = ["tan", "stand", "at"];
    [2.614]
    [2.655]
    let inputs = ["tan", "stand", "at"];
  • replacement in rust/anagram/tests/anagram.rs at line 29
    [2.656][2.656:687]()
    let outputs = vec!["tan"];
    [2.656]
    [2.687]
    let outputs = vec!["tan"];
  • replacement in rust/anagram/tests/anagram.rs at line 31
    [2.688][2.688:739]()
    process_anagram_case(word, &inputs, &outputs);
    [2.688]
    [2.739]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 37
    [2.810][2.810:834]()
    let word = "galea";
    [2.810]
    [2.834]
    let word = "galea";
  • replacement in rust/anagram/tests/anagram.rs at line 39
    [2.835][2.835:863]()
    let inputs = ["eagle"];
    [2.835]
    [2.863]
    let inputs = ["eagle"];
  • replacement in rust/anagram/tests/anagram.rs at line 41
    [2.864][2.864:890]()
    let outputs = vec![];
    [2.864]
    [2.890]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 43
    [2.891][2.891:942]()
    process_anagram_case(word, &inputs, &outputs);
    [2.891]
    [2.942]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 49
    [2.1001][2.1001:1024]()
    let word = "good";
    [2.1001]
    [2.1024]
    let word = "good";
  • replacement in rust/anagram/tests/anagram.rs at line 51
    [2.1025][2.1025:1060]()
    let inputs = ["dog", "goody"];
    [2.1025]
    [2.1060]
    let inputs = ["dog", "goody"];
  • replacement in rust/anagram/tests/anagram.rs at line 53
    [2.1061][2.1061:1087]()
    let outputs = vec![];
    [2.1061]
    [2.1087]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 55
    [2.1088][2.1088:1139]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1088]
    [2.1139]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 61
    [2.1187][2.1187:1212]()
    let word = "listen";
    [2.1187]
    [2.1212]
    let word = "listen";
  • replacement in rust/anagram/tests/anagram.rs at line 63
    [2.1213][2.1213:1273]()
    let inputs = ["enlists", "google", "inlets", "banana"];
    [2.1213]
    [2.1273]
    let inputs = ["enlists", "google", "inlets", "banana"];
  • replacement in rust/anagram/tests/anagram.rs at line 65
    [2.1274][2.1274:1308]()
    let outputs = vec!["inlets"];
    [2.1274]
    [2.1308]
    let outputs = vec!["inlets"];
  • replacement in rust/anagram/tests/anagram.rs at line 67
    [2.1309][2.1309:1360]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1309]
    [2.1360]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 73
    [2.1411][2.1411:1437]()
    let word = "allergy";
    [2.1411]
    [2.1437]
    let word = "allergy";
  • replacement in rust/anagram/tests/anagram.rs at line 75
    [2.1438][2.1438:1579]()
    let inputs = [
    "gallery",
    "ballerina",
    "regally",
    "clergy",
    "largely",
    "leading",
    ];
    [2.1438]
    [2.1579]
    let inputs = [
    "gallery",
    "ballerina",
    "regally",
    "clergy",
    "largely",
    "leading",
    ];
  • replacement in rust/anagram/tests/anagram.rs at line 84
    [2.1580][2.1580:1637]()
    let outputs = vec!["gallery", "regally", "largely"];
    [2.1580]
    [2.1637]
    let outputs = vec!["gallery", "regally", "largely"];
  • replacement in rust/anagram/tests/anagram.rs at line 86
    [2.1638][2.1638:1689]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1638]
    [2.1689]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 92
    [2.1748][2.1748:1776]()
    let word = "Orchestra";
    [2.1748]
    [2.1776]
    let word = "Orchestra";
  • replacement in rust/anagram/tests/anagram.rs at line 94
    [2.1777][2.1777:1837]()
    let inputs = ["cashregister", "Carthorse", "radishes"];
    [2.1777]
    [2.1837]
    let inputs = ["cashregister", "Carthorse", "radishes"];
  • replacement in rust/anagram/tests/anagram.rs at line 96
    [2.1838][2.1838:1875]()
    let outputs = vec!["Carthorse"];
    [2.1838]
    [2.1875]
    let outputs = vec!["Carthorse"];
  • replacement in rust/anagram/tests/anagram.rs at line 98
    [2.1876][2.1876:1927]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1876]
    [2.1927]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 104
    [2.1977][2.1977:2002]()
    let word = "ΑΒΓ";
    [2.1977]
    [2.2002]
    let word = "ΑΒΓ";
  • replacement in rust/anagram/tests/anagram.rs at line 106
    [2.2003][2.2003:2134]()
    // These words don't make sense, they're just greek letters cobbled together.
    let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"];
    [2.2003]
    [2.2134]
    // These words don't make sense, they're just greek letters cobbled together.
    let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"];
  • replacement in rust/anagram/tests/anagram.rs at line 109
    [2.2135][2.2135:2179]()
    let outputs = vec!["ΒΓΑ", "γβα"];
    [2.2135]
    [2.2179]
    let outputs = vec!["ΒΓΑ", "γβα"];
  • replacement in rust/anagram/tests/anagram.rs at line 111
    [2.2180][2.2180:2231]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2180]
    [2.2231]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 117
    [2.2292][2.2292:2489]()
    // 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 = "ΑΒΓ";
    [2.2292]
    [2.2489]
    // 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 = "ΑΒΓ";
  • replacement in rust/anagram/tests/anagram.rs at line 121
    [2.2490][2.2490:2517]()
    let inputs = ["ABΓ"];
    [2.2490]
    [2.2517]
    let inputs = ["ABΓ"];
  • replacement in rust/anagram/tests/anagram.rs at line 123
    [2.2518][2.2518:2544]()
    let outputs = vec![];
    [2.2518]
    [2.2544]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 125
    [2.2545][2.2545:2596]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2545]
    [2.2596]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 131
    [2.2671][2.2671:2696]()
    let word = "banana";
    [2.2671]
    [2.2696]
    let word = "banana";
  • replacement in rust/anagram/tests/anagram.rs at line 133
    [2.2697][2.2697:2726]()
    let inputs = ["banana"];
    [2.2697]
    [2.2726]
    let inputs = ["banana"];
  • replacement in rust/anagram/tests/anagram.rs at line 135
    [2.2727][2.2727:2753]()
    let outputs = vec![];
    [2.2727]
    [2.2753]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 137
    [2.2754][2.2754:2805]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2754]
    [2.2805]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 143
    [2.2898][2.2898:2923]()
    let word = "banana";
    [2.2898]
    [2.2923]
    let word = "banana";
  • replacement in rust/anagram/tests/anagram.rs at line 145
    [2.2924][2.2924:2953]()
    let inputs = ["bAnana"];
    [2.2924]
    [2.2953]
    let inputs = ["bAnana"];
  • replacement in rust/anagram/tests/anagram.rs at line 147
    [2.2954][2.2954:2980]()
    let outputs = vec![];
    [2.2954]
    [2.2980]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 149
    [2.2981][2.2981:3032]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2981]
    [2.3032]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 155
    [2.3133][2.3133:3158]()
    let word = "ΑΒΓ";
    [2.3133]
    [2.3158]
    let word = "ΑΒΓ";
  • replacement in rust/anagram/tests/anagram.rs at line 157
    [2.3159][2.3159:3188]()
    let inputs = ["ΑΒγ"];
    [2.3159]
    [2.3188]
    let inputs = ["ΑΒγ"];
  • replacement in rust/anagram/tests/anagram.rs at line 159
    [2.3189][2.3189:3215]()
    let outputs = vec![];
    [2.3189]
    [2.3215]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 161
    [2.3216][2.3216:3267]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3216]
    [2.3267]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 167
    [2.3327][2.3327:3365]()
    let word = "a⬂"; // 61 E2 AC 82
    [2.3327]
    [2.3365]
    let word = "a⬂"; // 61 E2 AC 82
  • replacement in rust/anagram/tests/anagram.rs at line 169
    [2.3366][2.3366:3408]()
    let inputs = ["€a"]; // E2 82 AC 61
    [2.3366]
    [2.3408]
    let inputs = ["€a"]; // E2 82 AC 61
  • replacement in rust/anagram/tests/anagram.rs at line 171
    [2.3409][2.3409:3435]()
    let outputs = vec![];
    [2.3409]
    [2.3435]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 173
    [2.3436][2.3436:3487]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3436]
    [2.3487]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 179
    [2.3555][2.3555:3576]()
    let word = "bc";
    [2.3555]
    [2.3576]
    let word = "bc";
  • replacement in rust/anagram/tests/anagram.rs at line 181
    [2.3577][2.3577:3602]()
    let inputs = ["ad"];
    [2.3577]
    [2.3602]
    let inputs = ["ad"];
  • replacement in rust/anagram/tests/anagram.rs at line 183
    [2.3603][2.3603:3629]()
    let outputs = vec![];
    [2.3603]
    [2.3629]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 185
    [2.3630][2.3630:3681]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3630]
    [2.3681]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/src/lib.rs at line 1
    [2.3731][2.3732:3763]()
    use std::collections::HashSet;
    [2.3731]
    [2.3763]
    use std::collections::{HashMap, HashSet};
    use unicode_segmentation::UnicodeSegmentation;
    fn checksum(word: &str) -> u8 {
    word.bytes()
    .fold(0, |accumulator, byte| accumulator.overflowing_add(byte).0)
    }
  • replacement in rust/anagram/src/lib.rs at line 9
    [2.3764][2.3764:3992]()
    pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&str]) -> HashSet<&'a str> {
    unimplemented!(
    "For the '{}' word find anagrams among the following words: {:?}",
    word,
    possible_anagrams
    );
    [2.3764]
    [2.3992]
    fn grapheme_histogram(word: &str) -> HashMap<&str, usize> {
    word.graphemes(true).fold(
    HashMap::with_capacity(word.len()),
    |mut hashmap, grapheme| {
    *hashmap.entry(grapheme).or_insert(0) += 1;
    hashmap
    },
    )
  • edit in rust/anagram/src/lib.rs at line 18
    [2.3994]
    pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
    let word_lowercased = word.to_lowercase();
    let (word_checksum, word_histogram) = (
    checksum(&word_lowercased),
    grapheme_histogram(&word_lowercased),
    );
    possible_anagrams
    .iter()
    .filter(|possible_anagram| {
    word.len() == possible_anagram.len() && {
    let possible_anagram = possible_anagram.to_lowercase();
    word_lowercased != possible_anagram
    && word_checksum == checksum(&possible_anagram)
    && word_histogram == grapheme_histogram(&possible_anagram)
    }
    })
    .copied()
    .collect()
    }
  • edit in rust/anagram/Cargo.toml at line 5
    [2.9068]
    [dependencies]
    unicode-segmentation = "1.8"