|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +/// Function that returns the letters that are missing from the input slice |
| 4 | +/// and are present in the English alphabet |
| 5 | +/// |
| 6 | +/// ## Arguments |
| 7 | +/// |
| 8 | +/// * `in_str` - the slice that will be checked for missing characters |
| 9 | +/// |
| 10 | +fn compute_missing(in_str: &str) -> HashSet<char> { |
| 11 | + let alphabet: HashSet<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect(); |
| 12 | + |
| 13 | + let letters_used: HashSet<char> = in_str |
| 14 | + .to_lowercase() |
| 15 | + .chars() |
| 16 | + .filter(|c| c.is_ascii_alphabetic()) |
| 17 | + .collect(); |
| 18 | + |
| 19 | + alphabet.difference(&letters_used).cloned().collect() |
| 20 | +} |
| 21 | + |
| 22 | +/// Function that checks if the slice is a lipogram with specific missing letters. |
| 23 | +/// Lipogram - sentence in which a particular letter or group of letters is avoided |
| 24 | +/// |
| 25 | +/// ## Arguments |
| 26 | +/// |
| 27 | +/// * `lipogram_str` - the slice that will be checked if is a lipogram with specific missing letters |
| 28 | +/// * `missing_chars` - the characters that has to be missing |
| 29 | +/// |
| 30 | +/// ## Examples |
| 31 | +/// |
| 32 | +/// ``` |
| 33 | +/// use the_algorithms_rust::string::is_lipogram; |
| 34 | +/// use std::collections::HashSet; |
| 35 | +/// |
| 36 | +/// assert!( |
| 37 | +/// !is_lipogram("The quick brown fox jumps over the lazy dog", |
| 38 | +/// &HashSet::from(['x']) |
| 39 | +/// )); |
| 40 | +/// |
| 41 | +/// assert!( |
| 42 | +/// is_lipogram("The brown cat jumped over the lazy dog with a brick", |
| 43 | +/// &HashSet::from(['f', 'q', 's', 'x']) |
| 44 | +/// )); |
| 45 | +/// |
| 46 | +/// assert!( |
| 47 | +/// !is_lipogram("The quick brown fox jumped over the lazy dog", |
| 48 | +/// &HashSet::from(['x']) |
| 49 | +/// )); |
| 50 | +/// ``` |
| 51 | +pub fn is_lipogram(lipogram_str: &str, missing_chars: &HashSet<char>) -> bool { |
| 52 | + if !missing_chars.iter().all(|&c| c.is_lowercase()) { |
| 53 | + panic!("missing_chars should be all lowercase.") |
| 54 | + } |
| 55 | + |
| 56 | + missing_chars == &compute_missing(lipogram_str) |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + macro_rules! test_lipogram { |
| 63 | + ($($name:ident: $inputs:expr,)*) => { |
| 64 | + $( |
| 65 | + #[test] |
| 66 | + fn $name() { |
| 67 | + let (in_str, missing_chars, other_chars) = $inputs; |
| 68 | + assert_ne!(missing_chars, other_chars); |
| 69 | + assert_eq!(compute_missing(in_str), missing_chars); |
| 70 | + assert!(is_lipogram(in_str, &missing_chars)); |
| 71 | + assert!(!is_lipogram(in_str, &other_chars)); |
| 72 | + } |
| 73 | + )* |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + test_lipogram! { |
| 78 | + lipogram1: ("The quick brown fox jumps over the lazy dog", HashSet::from([]), HashSet::from(['a', 'b'])), |
| 79 | + lipogram2: ("Jackdaws love my big sphinx of quartz", HashSet::from([]), HashSet::from(['x'])), |
| 80 | + lipogram3: ("abcdefghijklmnopqrstuvwxyz", HashSet::from([]), HashSet::from(['x', 'y', 'z'])), |
| 81 | + lipogram4: ("Five quacking zephyrs jolt my wax bed", HashSet::from([]), HashSet::from(['a'])), |
| 82 | + lipogram5: ("The quick brown fox jumped over the lazy dog", HashSet::from(['s']), HashSet::from([])), |
| 83 | + lipogram6: ("abcdefghijklmnopqrstuvwxy", HashSet::from(['z']), HashSet::from(['y', 'z'])), |
| 84 | + lipogram7: ("The brown fox jumped over the lazy dog with a brick", HashSet::from(['q', 's']), HashSet::from(['b'])), |
| 85 | + lipogram8: ("ABCdefghijklmnopqrstuvwx", HashSet::from(['y', 'z']), HashSet::from(['a', 'b'])), |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + #[should_panic] |
| 90 | + fn test_is_lipogram_panics_when_missing_chars_are_upper_case() { |
| 91 | + is_lipogram("abcdefghijklmnopqrstuvwx", &HashSet::from(['y', 'Z'])); |
| 92 | + } |
| 93 | +} |
0 commit comments