Skip to content

Instantly share code, notes, and snippets.

@jonfk
Created March 15, 2019 03:14
Show Gist options
  • Save jonfk/ad11be15e3692e3d7a1158db851a3a30 to your computer and use it in GitHub Desktop.
Save jonfk/ad11be15e3692e3d7a1158db851a3a30 to your computer and use it in GitHub Desktop.
fn pig_latin_word(word: &str) -> String {
let mut chars_iter = word.chars();
let mut first_last = if let Some(first) = chars_iter.next() {
let mut rest: String = chars_iter.collect();
rest.push(first);
rest
} else {
"".to_owned()
};
add_to_end(&first_last, "ay")
}
#[test]
fn test_pig_latin_word() {
let values = vec![
("hello", "ellohay"),
("world", "orldway"),
("ab", "baay"),
("a", "aay"),
("", "ay"),
];
for (input, expected_res) in values {
let res = pig_latin_word(input);
assert_eq!(res, expected_res);
}
}
fn pig_latin_phrase<F: Fn(&str) -> String>(phrase: &str, pig_latin_word_fn: F) -> String {
phrase
.split_whitespace()
.map(|word| pig_latin_word_fn(word))
.collect::<Vec<String>>()
.join(" ")
}
#[test]
fn test_pig_latin_phrase() {
let values = vec![("hello world", "ellohay orldway"), ("ab a", "baay aay")];
for (input, expected_res) in values {
let res = pig_latin_phrase(input, pig_latin_word);
assert_eq!(res, expected_res);
}
}
fn pig_latin_phrase_capitalized(phrase: &str) -> String {
pig_latin_phrase(phrase, pig_latin_word_capitalized)
}
fn pig_latin_word_capitalized(word: &str) -> String {
let mut chars_iter = word.chars();
let mut first_last = if let Some(first) = chars_iter.next() {
let first_decap = if first.is_uppercase() {
first.to_ascii_lowercase()
} else {
first
};
let mut rest: String = chars_iter.collect();
rest.push(first_decap);
capitalize_first_char(&rest)
} else {
"".to_owned()
};
first_last.push_str("ay");
first_last
}
fn capitalize_first_char(word: &str) -> String {
let owned_word = word.to_owned();
let mut word_iter = owned_word.chars();
if let Some(first) = word_iter.next() {
let rest: String = word_iter.collect();
let mut new_string = String::new();
new_string.push(first.to_ascii_uppercase());
new_string.push_str(&rest);
new_string
} else {
owned_word.clone()
}
}
#[test]
fn test_pig_latin_phrase_capitalized() {
let values = vec![("hello world", "Ellohay Orldway"), ("ab a", "Baay Aay")];
for (input, expected_res) in values {
let res = pig_latin_phrase_capitalized(input);
assert_eq!(res, expected_res);
}
}
fn pig_latin_phrase_capitalized_first_word_only(phrase: &str) -> String {
let acc: Vec<String> = phrase.split_whitespace().fold(Vec::new(), |mut acc, word| {
if acc.len() == 0 {
acc.push(pig_latin_word_capitalized(word));
} else {
acc.push(pig_latin_word(word));
}
acc
});
acc.join(", ")
}
#[test]
fn test_pig_latin_phrase_capitalized_first_word_only() {
let values = vec![("hello world", "Ellohay, orldway"), ("ab a", "Baay, aay")];
for (input, expected_res) in values {
let res = pig_latin_phrase_capitalized_first_word_only(input);
assert_eq!(res, expected_res);
}
}
fn pig_latin_word_no_reorder(word: &str) -> String {
add_to_end(word, "ay")
}
fn add_to_end(word: &str, postfix: &str) -> String {
let mut owned_word = word.to_owned();
owned_word.push_str(postfix);
owned_word
}
fn pig_latin_word_n_chars(word: &str, n: usize) -> String {
let owned_word = word.to_owned();
let mut chars: Vec<char> = owned_word.chars().collect();
let mut i = 0;
if chars.len() > n {
while i < n {
let first = chars.remove(0);
chars.push(first);
i += 1
}
let n_rev_word: String = chars.into_iter().collect();
add_to_end(&n_rev_word, "ay")
} else {
add_to_end(word, "ay")
}
}
#[test]
fn test_pig_latin_phrase_n_word_reorder() {
let values = vec![("dragons strike quickly", "agonsdray rikestay icklyquay")];
for (input, expected_res) in values {
let res = pig_latin_phrase(input, |x| pig_latin_word_n_chars(x, 2));
assert_eq!(res, expected_res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment