Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. fn pig_latin_word(word: &str) -> String {
  2. let mut chars_iter = word.chars();
  3.  
  4. let mut first_last = if let Some(first) = chars_iter.next() {
  5. let mut rest: String = chars_iter.collect();
  6. rest.push(first);
  7. rest
  8. } else {
  9. "".to_owned()
  10. };
  11. add_to_end(&first_last, "ay")
  12. }
  13.  
  14. #[test]
  15. fn test_pig_latin_word() {
  16. let values = vec![
  17. ("hello", "ellohay"),
  18. ("world", "orldway"),
  19. ("ab", "baay"),
  20. ("a", "aay"),
  21. ("", "ay"),
  22. ];
  23.  
  24. for (input, expected_res) in values {
  25. let res = pig_latin_word(input);
  26. assert_eq!(res, expected_res);
  27. }
  28. }
  29.  
  30. fn pig_latin_phrase<F: Fn(&str) -> String>(phrase: &str, pig_latin_word_fn: F) -> String {
  31. phrase
  32. .split_whitespace()
  33. .map(|word| pig_latin_word_fn(word))
  34. .collect::<Vec<String>>()
  35. .join(" ")
  36. }
  37.  
  38. #[test]
  39. fn test_pig_latin_phrase() {
  40. let values = vec![("hello world", "ellohay orldway"), ("ab a", "baay aay")];
  41.  
  42. for (input, expected_res) in values {
  43. let res = pig_latin_phrase(input, pig_latin_word);
  44. assert_eq!(res, expected_res);
  45. }
  46. }
  47.  
  48. fn pig_latin_phrase_capitalized(phrase: &str) -> String {
  49. pig_latin_phrase(phrase, pig_latin_word_capitalized)
  50. }
  51.  
  52. fn pig_latin_word_capitalized(word: &str) -> String {
  53. let mut chars_iter = word.chars();
  54.  
  55. let mut first_last = if let Some(first) = chars_iter.next() {
  56. let first_decap = if first.is_uppercase() {
  57. first.to_ascii_lowercase()
  58. } else {
  59. first
  60. };
  61.  
  62. let mut rest: String = chars_iter.collect();
  63.  
  64. rest.push(first_decap);
  65. capitalize_first_char(&rest)
  66. } else {
  67. "".to_owned()
  68. };
  69. first_last.push_str("ay");
  70. first_last
  71. }
  72.  
  73. fn capitalize_first_char(word: &str) -> String {
  74. let owned_word = word.to_owned();
  75. let mut word_iter = owned_word.chars();
  76.  
  77. if let Some(first) = word_iter.next() {
  78. let rest: String = word_iter.collect();
  79. let mut new_string = String::new();
  80. new_string.push(first.to_ascii_uppercase());
  81. new_string.push_str(&rest);
  82. new_string
  83. } else {
  84. owned_word.clone()
  85. }
  86. }
  87.  
  88. #[test]
  89. fn test_pig_latin_phrase_capitalized() {
  90. let values = vec![("hello world", "Ellohay Orldway"), ("ab a", "Baay Aay")];
  91.  
  92. for (input, expected_res) in values {
  93. let res = pig_latin_phrase_capitalized(input);
  94. assert_eq!(res, expected_res);
  95. }
  96. }
  97.  
  98. fn pig_latin_phrase_capitalized_first_word_only(phrase: &str) -> String {
  99. let acc: Vec<String> = phrase.split_whitespace().fold(Vec::new(), |mut acc, word| {
  100. if acc.len() == 0 {
  101. acc.push(pig_latin_word_capitalized(word));
  102. } else {
  103. acc.push(pig_latin_word(word));
  104. }
  105. acc
  106. });
  107.  
  108. acc.join(", ")
  109. }
  110.  
  111. #[test]
  112. fn test_pig_latin_phrase_capitalized_first_word_only() {
  113. let values = vec![("hello world", "Ellohay, orldway"), ("ab a", "Baay, aay")];
  114.  
  115. for (input, expected_res) in values {
  116. let res = pig_latin_phrase_capitalized_first_word_only(input);
  117. assert_eq!(res, expected_res);
  118. }
  119. }
  120.  
  121. fn pig_latin_word_no_reorder(word: &str) -> String {
  122. add_to_end(word, "ay")
  123. }
  124.  
  125. fn add_to_end(word: &str, postfix: &str) -> String {
  126. let mut owned_word = word.to_owned();
  127. owned_word.push_str(postfix);
  128. owned_word
  129. }
  130.  
  131. fn pig_latin_word_n_chars(word: &str, n: usize) -> String {
  132. let owned_word = word.to_owned();
  133. let mut chars: Vec<char> = owned_word.chars().collect();
  134. let mut i = 0;
  135.  
  136. if chars.len() > n {
  137. while i < n {
  138. let first = chars.remove(0);
  139. chars.push(first);
  140. i += 1
  141. }
  142. let n_rev_word: String = chars.into_iter().collect();
  143. add_to_end(&n_rev_word, "ay")
  144. } else {
  145. add_to_end(word, "ay")
  146. }
  147. }
  148.  
  149. #[test]
  150. fn test_pig_latin_phrase_n_word_reorder() {
  151. let values = vec![("dragons strike quickly", "agonsdray rikestay icklyquay")];
  152.  
  153. for (input, expected_res) in values {
  154. let res = pig_latin_phrase(input, |x| pig_latin_word_n_chars(x, 2));
  155. assert_eq!(res, expected_res);
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement