Skip to content

Commit

Permalink
Implement pangram
Browse files Browse the repository at this point in the history
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
whit0694 committed Jun 3, 2016
1 parent e57699d commit adc92c5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"difference-of-squares",
"hamming",
"scrabble-score",
"pangram",
"nucleotide-count",
"word-count",
"etl",
Expand Down
4 changes: 4 additions & 0 deletions exercises/pangram/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exercises/pangram/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "pangram"
version = "0.0.0"
18 changes: 18 additions & 0 deletions exercises/pangram/example.rs
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";
65 changes: 65 additions & 0 deletions exercises/pangram/tests/pangram.rs
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));
}

0 comments on commit adc92c5

Please sign in to comment.