-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the Pangram problem and follows the current canonical test suite. I'm putting the problem after scrabble score. I'm guessing that Most implementations will use iter -> filter -> collect (though students constantly surprise me with their great ideas). The track places these iterator -> Higher Order Function problems around scrabble score & hamming.
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[package] | ||
name = "pangram" | ||
version = "0.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use std::collections::BTreeSet; | ||
use std::ascii::AsciiExt; | ||
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
extern crate pangram; | ||
|
||
use pangram::*; | ||
|
||
#[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 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_numbers() { | ||
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)); | ||
} |