Pangram
1. Readme
全字母句(pangram)
判断句子是否是全字母句。全字母句(希腊语:παγρμμα,pan 语法,”每个字母”)是一个使用字母表中每个字母,至少一次的句子。最著名的英语是 全字母句:
The quick brown fox jumps over the lazy dog.
字母表由 ASCII 字母a
到z
的全部组成,并且不区分大小写。输入不能包含非 ASCII 符号.
资源
维基百科https://en.wikipedia.org/wiki/Pangram
2. 开始你的表演
/// Determine whether a sentence is a pangram. pub fn is_pangram(sentence: &str) -> bool { unimplemented!("Is {} a pangram?", sentence); }
3. 测试代码查看
# #![allow(unused_variables)] #fn main() { #[test] fn empty_strings_are_not_pangrams() { let sentence = ""; assert!(!is_pangram(&sentence)); } #[test] //#[ignore] fn classic_pangram_is_a_pangram() { let sentence = "the quick brown fox jumps over the lazy dog"; assert!(is_pangram(&sentence)); } #[test] //#[ignore] fn pangrams_must_have_all_letters() { let sentence = "a quick movement of the enemy will jeopardize five gunboats"; assert!(!is_pangram(&sentence)); } #[test] //#[ignore] fn pangrams_must_have_all_letters_two() { let sentence = "the quick brown fish jumps over the lazy dog"; assert!(!is_pangram(&sentence)); } #[test] //#[ignore] fn pangrams_must_include_z() { let sentence = "the quick brown fox jumps over the lay dog"; assert!(!is_pangram(&sentence)); } #[test] //#[ignore] fn underscores_do_not_affect_pangrams() { let sentence = "the_quick_brown_fox_jumps_over_the_lazy_dog"; assert!(is_pangram(&sentence)); } #[test] //#[ignore] fn numbers_do_not_affect_pangrams() { let sentence = "the 1 quick brown fox jumps over the 2 lazy dogs"; assert!(is_pangram(&sentence)); } #[test] //#[ignore] fn numbers_can_not_replace_letters() { let sentence = "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"; assert!(!is_pangram(&sentence)); } #[test] //#[ignore] fn capitals_and_punctuation_can_be_in_pangrams() { let sentence = "\"Five quacking Zephyrs jolt my wax bed.\""; assert!(is_pangram(&sentence)); } #[test] //#[ignore] fn non_ascii_characters_can_be_in_pangrams() { let sentence = "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich."; assert!(is_pangram(&sentence)); } #}
4. 答案
# #![allow(unused_variables)] #fn main() { use std::collections::BTreeSet; use std::iter::FromIterator; pub fn is_pangram(sentence: &str) -> bool { sentence .to_lowercase() .chars() .filter(|c| c.is_alphabetic()) .filter(|c| c.is_ascii()) .collect::<BTreeSet<char>>() == english_letter_set() } fn english_letter_set() -> BTreeSet<char> { BTreeSet::from_iter(ENGLISH_ALPHABET.chars()) } const ENGLISH_ALPHABET: &'static str = "abcdefghijklmnopqrstuvwxyz"; #}