naive implementation of anagram

This commit is contained in:
Blizzard Finnegan 2023-08-21 15:20:27 -04:00
parent 988ca31e2b
commit bbe4e7fbc3
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
2 changed files with 24 additions and 8 deletions

View file

@ -1,9 +1,27 @@
use std::collections::HashSet;
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
);
pub fn anagrams_for<'a>(word: &str, possible_anagrams: &[&'a str]) -> HashSet<&'a str> {
let mut word_set = HashSet::new();
for char in word.chars(){
word_set.insert(char.clone().to_ascii_uppercase());
word_set.insert(char.clone().to_ascii_lowercase());
}
let mut output:HashSet<&'a str> = HashSet::new();
for possible_anagram in possible_anagrams.iter(){
if *possible_anagram == word{ continue; }
let mut anagram_set = word_set.clone();
for char in possible_anagram.chars(){
if anagram_set.remove(&char){
anagram_set.remove(&char.to_ascii_uppercase());
anagram_set.remove(&char.to_ascii_lowercase());
}
else{
break;
}
}
if anagram_set.len() == 0{
output.insert(&possible_anagram);
}
}
output
}

View file

@ -20,7 +20,6 @@ fn test_no_matches() {
}
#[test]
#[ignore]
fn test_detect_simple_anagram() {
let word = "ant";
@ -32,7 +31,6 @@ fn test_detect_simple_anagram() {
}
#[test]
#[ignore]
fn test_does_not_confuse_different_duplicates() {
let word = "galea";