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

[?]
9Zb2bmkejrNknawUtr3MKvVstZkVDR8x8ritfgZXKrky
Dec 23, 2021, 5:31 PM
2MPN73UJBT4AA44CVFCBRLB4BRO44BNASUXTFF3NZIEFN7EGENWQC

Dependencies

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

Change contents

  • replacement in rust/anagram/tests/anagram.rs at line 4
    [2.182][2.182:236]()
    let result = anagram::anagrams_for(word, inputs);
    [2.182]
    [2.236]
    let result = anagram::anagrams_for(word, inputs);
  • replacement in rust/anagram/tests/anagram.rs at line 6
    [2.237][2.237:307]()
    let expected: HashSet<&str> = expected.iter().cloned().collect();
    [2.237]
    [2.307]
    let expected: HashSet<&str> = expected.iter().cloned().collect();
  • replacement in rust/anagram/tests/anagram.rs at line 8
    [2.308][2.308:342]()
    assert_eq!(result, expected);
    [2.308]
    [2.342]
    assert_eq!(result, expected);
  • replacement in rust/anagram/tests/anagram.rs at line 13
    [2.376][2.376:401]()
    let word = "diaper";
    [2.376]
    [2.401]
    let word = "diaper";
  • replacement in rust/anagram/tests/anagram.rs at line 15
    [2.402][2.402:459]()
    let inputs = ["hello", "world", "zombies", "pants"];
    [2.402]
    [2.459]
    let inputs = ["hello", "world", "zombies", "pants"];
  • replacement in rust/anagram/tests/anagram.rs at line 17
    [2.460][2.460:486]()
    let outputs = vec![];
    [2.460]
    [2.486]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 19
    [2.487][2.487:538]()
    process_anagram_case(word, &inputs, &outputs);
    [2.487]
    [2.538]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 25
    [2.593][2.593:615]()
    let word = "ant";
    [2.593]
    [2.615]
    let word = "ant";
  • replacement in rust/anagram/tests/anagram.rs at line 27
    [2.616][2.616:657]()
    let inputs = ["tan", "stand", "at"];
    [2.616]
    [2.657]
    let inputs = ["tan", "stand", "at"];
  • replacement in rust/anagram/tests/anagram.rs at line 29
    [2.658][2.658:689]()
    let outputs = vec!["tan"];
    [2.658]
    [2.689]
    let outputs = vec!["tan"];
  • replacement in rust/anagram/tests/anagram.rs at line 31
    [2.690][2.690:741]()
    process_anagram_case(word, &inputs, &outputs);
    [2.690]
    [2.741]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 37
    [2.812][2.812:836]()
    let word = "galea";
    [2.812]
    [2.836]
    let word = "galea";
  • replacement in rust/anagram/tests/anagram.rs at line 39
    [2.837][2.837:865]()
    let inputs = ["eagle"];
    [2.837]
    [2.865]
    let inputs = ["eagle"];
  • replacement in rust/anagram/tests/anagram.rs at line 41
    [2.866][2.866:892]()
    let outputs = vec![];
    [2.866]
    [2.892]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 43
    [2.893][2.893:944]()
    process_anagram_case(word, &inputs, &outputs);
    [2.893]
    [2.944]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 49
    [2.1003][2.1003:1026]()
    let word = "good";
    [2.1003]
    [2.1026]
    let word = "good";
  • replacement in rust/anagram/tests/anagram.rs at line 51
    [2.1027][2.1027:1062]()
    let inputs = ["dog", "goody"];
    [2.1027]
    [2.1062]
    let inputs = ["dog", "goody"];
  • replacement in rust/anagram/tests/anagram.rs at line 53
    [2.1063][2.1063:1089]()
    let outputs = vec![];
    [2.1063]
    [2.1089]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 55
    [2.1090][2.1090:1141]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1090]
    [2.1141]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 61
    [2.1189][2.1189:1214]()
    let word = "listen";
    [2.1189]
    [2.1214]
    let word = "listen";
  • replacement in rust/anagram/tests/anagram.rs at line 63
    [2.1215][2.1215:1275]()
    let inputs = ["enlists", "google", "inlets", "banana"];
    [2.1215]
    [2.1275]
    let inputs = ["enlists", "google", "inlets", "banana"];
  • replacement in rust/anagram/tests/anagram.rs at line 65
    [2.1276][2.1276:1310]()
    let outputs = vec!["inlets"];
    [2.1276]
    [2.1310]
    let outputs = vec!["inlets"];
  • replacement in rust/anagram/tests/anagram.rs at line 67
    [2.1311][2.1311:1362]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1311]
    [2.1362]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 73
    [2.1413][2.1413:1439]()
    let word = "allergy";
    [2.1413]
    [2.1439]
    let word = "allergy";
  • replacement in rust/anagram/tests/anagram.rs at line 75
    [2.1440][2.1440:1581]()
    let inputs = [
    "gallery",
    "ballerina",
    "regally",
    "clergy",
    "largely",
    "leading",
    ];
    [2.1440]
    [2.1581]
    let inputs = [
    "gallery",
    "ballerina",
    "regally",
    "clergy",
    "largely",
    "leading",
    ];
  • replacement in rust/anagram/tests/anagram.rs at line 84
    [2.1582][2.1582:1639]()
    let outputs = vec!["gallery", "regally", "largely"];
    [2.1582]
    [2.1639]
    let outputs = vec!["gallery", "regally", "largely"];
  • replacement in rust/anagram/tests/anagram.rs at line 86
    [2.1640][2.1640:1691]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1640]
    [2.1691]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 92
    [2.1750][2.1750:1778]()
    let word = "Orchestra";
    [2.1750]
    [2.1778]
    let word = "Orchestra";
  • replacement in rust/anagram/tests/anagram.rs at line 94
    [2.1779][2.1779:1839]()
    let inputs = ["cashregister", "Carthorse", "radishes"];
    [2.1779]
    [2.1839]
    let inputs = ["cashregister", "Carthorse", "radishes"];
  • replacement in rust/anagram/tests/anagram.rs at line 96
    [2.1840][2.1840:1877]()
    let outputs = vec!["Carthorse"];
    [2.1840]
    [2.1877]
    let outputs = vec!["Carthorse"];
  • replacement in rust/anagram/tests/anagram.rs at line 98
    [2.1878][2.1878:1929]()
    process_anagram_case(word, &inputs, &outputs);
    [2.1878]
    [2.1929]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 104
    [2.1979][2.1979:2004]()
    let word = "ΑΒΓ";
    [2.1979]
    [2.2004]
    let word = "ΑΒΓ";
  • replacement in rust/anagram/tests/anagram.rs at line 106
    [2.2005][2.2005:2136]()
    // These words don't make sense, they're just greek letters cobbled together.
    let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"];
    [2.2005]
    [2.2136]
    // 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.2137][2.2137:2181]()
    let outputs = vec!["ΒΓΑ", "γβα"];
    [2.2137]
    [2.2181]
    let outputs = vec!["ΒΓΑ", "γβα"];
  • replacement in rust/anagram/tests/anagram.rs at line 111
    [2.2182][2.2182:2233]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2182]
    [2.2233]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 117
    [2.2294][2.2294:2491]()
    // 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.2294]
    [2.2491]
    // 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.2492][2.2492:2519]()
    let inputs = ["ABΓ"];
    [2.2492]
    [2.2519]
    let inputs = ["ABΓ"];
  • replacement in rust/anagram/tests/anagram.rs at line 123
    [2.2520][2.2520:2546]()
    let outputs = vec![];
    [2.2520]
    [2.2546]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 125
    [2.2547][2.2547:2598]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2547]
    [2.2598]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 131
    [2.2673][2.2673:2698]()
    let word = "banana";
    [2.2673]
    [2.2698]
    let word = "banana";
  • replacement in rust/anagram/tests/anagram.rs at line 133
    [2.2699][2.2699:2728]()
    let inputs = ["banana"];
    [2.2699]
    [2.2728]
    let inputs = ["banana"];
  • replacement in rust/anagram/tests/anagram.rs at line 135
    [2.2729][2.2729:2755]()
    let outputs = vec![];
    [2.2729]
    [2.2755]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 137
    [2.2756][2.2756:2807]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2756]
    [2.2807]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 143
    [2.2900][2.2900:2925]()
    let word = "banana";
    [2.2900]
    [2.2925]
    let word = "banana";
  • replacement in rust/anagram/tests/anagram.rs at line 145
    [2.2926][2.2926:2955]()
    let inputs = ["bAnana"];
    [2.2926]
    [2.2955]
    let inputs = ["bAnana"];
  • replacement in rust/anagram/tests/anagram.rs at line 147
    [2.2956][2.2956:2982]()
    let outputs = vec![];
    [2.2956]
    [2.2982]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 149
    [2.2983][2.2983:3034]()
    process_anagram_case(word, &inputs, &outputs);
    [2.2983]
    [2.3034]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 155
    [2.3135][2.3135:3160]()
    let word = "ΑΒΓ";
    [2.3135]
    [2.3160]
    let word = "ΑΒΓ";
  • replacement in rust/anagram/tests/anagram.rs at line 157
    [2.3161][2.3161:3190]()
    let inputs = ["ΑΒγ"];
    [2.3161]
    [2.3190]
    let inputs = ["ΑΒγ"];
  • replacement in rust/anagram/tests/anagram.rs at line 159
    [2.3191][2.3191:3217]()
    let outputs = vec![];
    [2.3191]
    [2.3217]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 161
    [2.3218][2.3218:3269]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3218]
    [2.3269]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 167
    [2.3329][2.3329:3367]()
    let word = "a⬂"; // 61 E2 AC 82
    [2.3329]
    [2.3367]
    let word = "a⬂"; // 61 E2 AC 82
  • replacement in rust/anagram/tests/anagram.rs at line 169
    [2.3368][2.3368:3410]()
    let inputs = ["€a"]; // E2 82 AC 61
    [2.3368]
    [2.3410]
    let inputs = ["€a"]; // E2 82 AC 61
  • replacement in rust/anagram/tests/anagram.rs at line 171
    [2.3411][2.3411:3437]()
    let outputs = vec![];
    [2.3411]
    [2.3437]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 173
    [2.3438][2.3438:3489]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3438]
    [2.3489]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/tests/anagram.rs at line 179
    [2.3557][2.3557:3578]()
    let word = "bc";
    [2.3557]
    [2.3578]
    let word = "bc";
  • replacement in rust/anagram/tests/anagram.rs at line 181
    [2.3579][2.3579:3604]()
    let inputs = ["ad"];
    [2.3579]
    [2.3604]
    let inputs = ["ad"];
  • replacement in rust/anagram/tests/anagram.rs at line 183
    [2.3605][2.3605:3631]()
    let outputs = vec![];
    [2.3605]
    [2.3631]
    let outputs = vec![];
  • replacement in rust/anagram/tests/anagram.rs at line 185
    [2.3632][2.3632:3683]()
    process_anagram_case(word, &inputs, &outputs);
    [2.3632]
    [2.3683]
    process_anagram_case(word, &inputs, &outputs);
  • replacement in rust/anagram/src/lib.rs at line 1
    [2.3734][2.3735:3766]()
    use std::collections::HashSet;
    [2.3734]
    [2.3766]
    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.3767][2.3767:3995]()
    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.3767]
    [2.3995]
    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.3997]
    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.9071]
    [dependencies]
    unicode-segmentation = "1.8"