mykatas/rust/tests/2_counting_duplicates.rs

33 lines
662 B
Rust
Raw Normal View History

2021-10-29 10:17:58 +03:00
use std::collections::HashMap;
fn count_duplicates(text: &str) -> u32 {
text.to_lowercase()
.chars()
.fold(HashMap::<char, u32>::new(), |mut hm, c| {
(*hm.entry(c).or_default()) += 1;
2021-10-29 10:17:58 +03:00
hm
})
.values()
.filter(|&&count| count > 1)
.count() as u32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_abcde() {
assert_eq!(count_duplicates("abcde"), 0);
}
#[test]
fn test_abcdea() {
assert_eq!(count_duplicates("abcdea"), 1);
}
#[test]
fn test_indivisibility() {
assert_eq!(count_duplicates("indivisibility"), 1);
}
}