anagram
This commit is contained in:
parent
62d1558abe
commit
498f64b5cd
3 changed files with 35 additions and 24 deletions
|
|
@ -1,8 +1,36 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
pub fn anagrams_for<'a>(
|
pub trait CharCounter {
|
||||||
word: &str,
|
fn get_chars_frequency(&self) -> HashMap<char, usize>;
|
||||||
possible_anagrams: &[&str]
|
}
|
||||||
) -> HashSet<&'a str> {
|
|
||||||
todo!("For the '{word}' word find anagrams among the following words: {possible_anagrams:?}");
|
pub trait AnagramsFinder<'a> {
|
||||||
|
fn find_anagrams(&self, pattern_raw: &str) -> HashSet<&'a str>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CharCounter for str {
|
||||||
|
fn get_chars_frequency(&self) -> HashMap<char, usize> {
|
||||||
|
self.chars().fold(HashMap::new(), |mut acc, c| {
|
||||||
|
*acc.entry(c).or_insert(0) += 1;
|
||||||
|
acc
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AnagramsFinder<'a> for [&'a str] {
|
||||||
|
fn find_anagrams(&self, pattern_raw: &str) -> HashSet<&'a str> {
|
||||||
|
let pattern = pattern_raw.to_lowercase();
|
||||||
|
let pattern_map: HashMap<char, usize> = pattern.get_chars_frequency();
|
||||||
|
self.iter()
|
||||||
|
.copied()
|
||||||
|
.filter(|word_raw| {
|
||||||
|
let word = word_raw.to_lowercase();
|
||||||
|
word != pattern && word.get_chars_frequency() == pattern_map
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
|
||||||
|
possible_anagrams.find_anagrams(word)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ fn no_matches() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_two_anagrams() {
|
fn detects_two_anagrams() {
|
||||||
let word = "solemn";
|
let word = "solemn";
|
||||||
let inputs = &["lemons", "cherry", "melons"];
|
let inputs = &["lemons", "cherry", "melons"];
|
||||||
|
|
@ -21,7 +20,6 @@ fn detects_two_anagrams() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn does_not_detect_anagram_subsets() {
|
fn does_not_detect_anagram_subsets() {
|
||||||
let word = "good";
|
let word = "good";
|
||||||
let inputs = &["dog", "goody"];
|
let inputs = &["dog", "goody"];
|
||||||
|
|
@ -31,7 +29,6 @@ fn does_not_detect_anagram_subsets() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_anagram() {
|
fn detects_anagram() {
|
||||||
let word = "listen";
|
let word = "listen";
|
||||||
let inputs = &["enlists", "google", "inlets", "banana"];
|
let inputs = &["enlists", "google", "inlets", "banana"];
|
||||||
|
|
@ -41,7 +38,6 @@ fn detects_anagram() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_three_anagrams() {
|
fn detects_three_anagrams() {
|
||||||
let word = "allergy";
|
let word = "allergy";
|
||||||
let inputs = &[
|
let inputs = &[
|
||||||
|
|
@ -58,7 +54,6 @@ fn detects_three_anagrams() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_multiple_anagrams_with_different_case() {
|
fn detects_multiple_anagrams_with_different_case() {
|
||||||
let word = "nose";
|
let word = "nose";
|
||||||
let inputs = &["Eons", "ONES"];
|
let inputs = &["Eons", "ONES"];
|
||||||
|
|
@ -68,7 +63,6 @@ fn detects_multiple_anagrams_with_different_case() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn does_not_detect_non_anagrams_with_identical_checksum() {
|
fn does_not_detect_non_anagrams_with_identical_checksum() {
|
||||||
let word = "mass";
|
let word = "mass";
|
||||||
let inputs = &["last"];
|
let inputs = &["last"];
|
||||||
|
|
@ -78,7 +72,6 @@ fn does_not_detect_non_anagrams_with_identical_checksum() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_anagrams_case_insensitively() {
|
fn detects_anagrams_case_insensitively() {
|
||||||
let word = "Orchestra";
|
let word = "Orchestra";
|
||||||
let inputs = &["cashregister", "Carthorse", "radishes"];
|
let inputs = &["cashregister", "Carthorse", "radishes"];
|
||||||
|
|
@ -88,7 +81,6 @@ fn detects_anagrams_case_insensitively() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_anagrams_using_case_insensitive_subject() {
|
fn detects_anagrams_using_case_insensitive_subject() {
|
||||||
let word = "Orchestra";
|
let word = "Orchestra";
|
||||||
let inputs = &["cashregister", "carthorse", "radishes"];
|
let inputs = &["cashregister", "carthorse", "radishes"];
|
||||||
|
|
@ -98,7 +90,6 @@ fn detects_anagrams_using_case_insensitive_subject() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn detects_anagrams_using_case_insensitive_possible_matches() {
|
fn detects_anagrams_using_case_insensitive_possible_matches() {
|
||||||
let word = "orchestra";
|
let word = "orchestra";
|
||||||
let inputs = &["cashregister", "Carthorse", "radishes"];
|
let inputs = &["cashregister", "Carthorse", "radishes"];
|
||||||
|
|
@ -108,7 +99,6 @@ fn detects_anagrams_using_case_insensitive_possible_matches() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn does_not_detect_an_anagram_if_the_original_word_is_repeated() {
|
fn does_not_detect_an_anagram_if_the_original_word_is_repeated() {
|
||||||
let word = "go";
|
let word = "go";
|
||||||
let inputs = &["goGoGO"];
|
let inputs = &["goGoGO"];
|
||||||
|
|
@ -118,7 +108,6 @@ fn does_not_detect_an_anagram_if_the_original_word_is_repeated() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn anagrams_must_use_all_letters_exactly_once() {
|
fn anagrams_must_use_all_letters_exactly_once() {
|
||||||
let word = "tapper";
|
let word = "tapper";
|
||||||
let inputs = &["patter"];
|
let inputs = &["patter"];
|
||||||
|
|
@ -128,7 +117,6 @@ fn anagrams_must_use_all_letters_exactly_once() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn words_are_not_anagrams_of_themselves() {
|
fn words_are_not_anagrams_of_themselves() {
|
||||||
let word = "BANANA";
|
let word = "BANANA";
|
||||||
let inputs = &["BANANA"];
|
let inputs = &["BANANA"];
|
||||||
|
|
@ -138,7 +126,6 @@ fn words_are_not_anagrams_of_themselves() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different() {
|
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different() {
|
||||||
let word = "BANANA";
|
let word = "BANANA";
|
||||||
let inputs = &["Banana"];
|
let inputs = &["Banana"];
|
||||||
|
|
@ -148,7 +135,6 @@ fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_differe
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different() {
|
fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different() {
|
||||||
let word = "BANANA";
|
let word = "BANANA";
|
||||||
let inputs = &["banana"];
|
let inputs = &["banana"];
|
||||||
|
|
@ -158,7 +144,6 @@ fn words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_differ
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn words_other_than_themselves_can_be_anagrams() {
|
fn words_other_than_themselves_can_be_anagrams() {
|
||||||
let word = "LISTEN";
|
let word = "LISTEN";
|
||||||
let inputs = &["LISTEN", "Silent"];
|
let inputs = &["LISTEN", "Silent"];
|
||||||
|
|
@ -168,7 +153,6 @@ fn words_other_than_themselves_can_be_anagrams() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn handles_case_of_greek_letters() {
|
fn handles_case_of_greek_letters() {
|
||||||
let word = "ΑΒΓ";
|
let word = "ΑΒΓ";
|
||||||
let inputs = &["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"];
|
let inputs = &["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"];
|
||||||
|
|
@ -178,7 +162,6 @@ fn handles_case_of_greek_letters() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn different_characters_may_have_the_same_bytes() {
|
fn different_characters_may_have_the_same_bytes() {
|
||||||
let word = "a⬂";
|
let word = "a⬂";
|
||||||
let inputs = &["€a"];
|
let inputs = &["€a"];
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ pub fn frequency(input: &[&str], worker_count: usize) -> HashMap<char, usize> {
|
||||||
|
|
||||||
thread::scope(|s| {
|
thread::scope(|s| {
|
||||||
let chunk_size = input.len().div_ceil(worker_count);
|
let chunk_size = input.len().div_ceil(worker_count);
|
||||||
let mut handles: Vec<_> = input
|
let handles: Vec<_> = input
|
||||||
.chunks(chunk_size)
|
.chunks(chunk_size)
|
||||||
.map(|chunk| {
|
.map(|chunk| {
|
||||||
s.spawn(move || {
|
s.spawn(move || {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue