Merge branch 'exercises/pig-latin'
This commit is contained in:
@@ -6,10 +6,15 @@ mod pig_latin;
|
||||
|
||||
fn main() {
|
||||
// Stats
|
||||
println!("Stats");
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2];
|
||||
let mut numbers = stats::NewStatsCalculator::from_vec(&data);
|
||||
println!("Data: {:?}", numbers);
|
||||
println!("Mean: {:?}", numbers.get_mean());
|
||||
println!("Median: {:?}", numbers.get_median());
|
||||
println!("Mode: {:?}", numbers.get_mode());
|
||||
|
||||
println!("\nPig Latin");
|
||||
println!("{}", pig_latin::pig_latin("rustacian"));
|
||||
println!("{}", pig_latin::pig_latin("awesome"));
|
||||
}
|
||||
|
||||
@@ -1,28 +1,54 @@
|
||||
// Take a &str
|
||||
// - assume single words (check for no spaces)
|
||||
// - convert to String
|
||||
// pop the first
|
||||
// append ay
|
||||
// move to end
|
||||
// Handle sentences: take a &str, convert to array, then do the above for each and return
|
||||
// a new array
|
||||
// https://scottwlaforest.com/rust/the-rust-book-chapter-8-exercises/
|
||||
|
||||
// NOTE: Check for vowels!
|
||||
|
||||
fn pig_latin(string: &str) -> String {
|
||||
pub fn pig_latin(string: &str) -> String {
|
||||
let s = String::from(string);
|
||||
println!("{:?}", s.chars());
|
||||
s
|
||||
let mut portion = String::new();
|
||||
let mut latinised = String::new();
|
||||
let mut starts_with_vowel = false;
|
||||
let vowels = ['a', 'e', 'i', 'o', 'u'];
|
||||
|
||||
if s.is_empty() {
|
||||
return s;
|
||||
}
|
||||
|
||||
let first_character = s.chars().next().unwrap();
|
||||
|
||||
if vowels.contains(&first_character) {
|
||||
portion.push_str("-hay");
|
||||
starts_with_vowel = true;
|
||||
} else {
|
||||
portion = format!("-{}ay", &first_character);
|
||||
}
|
||||
for (pos, char) in s.char_indices() {
|
||||
if pos == 0 && !starts_with_vowel {
|
||||
continue;
|
||||
}
|
||||
latinised.push(char);
|
||||
}
|
||||
latinised.push_str(&portion);
|
||||
latinised
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pig_latin() {
|
||||
let string = "rust❤";
|
||||
fn test_pig_latin_start_with_vowel() {
|
||||
let string = "apple";
|
||||
let result = pig_latin(string);
|
||||
println!("{}", result);
|
||||
assert_eq!(result, "apple-hay");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pig_latin_start_with_consonant() {
|
||||
let string = "first";
|
||||
let result = pig_latin(string);
|
||||
assert_eq!(result, "irst-fay");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pig_latin_empty_string() {
|
||||
let string = "";
|
||||
let result = pig_latin(string);
|
||||
assert_eq!(result, string)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user