Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. use std::convert::TryInto;
  2.  
  3. pub fn encode(mut s: String) -> String {
  4. s = s.replace(" ", "");
  5. s.make_ascii_lowercase();
  6. s = s.chars().filter_map(|c| atbash_cipher_substitute(c)).collect();
  7. add_atbash_whitespace(s)
  8. }
  9.  
  10. pub fn decode(mut s: String) -> String {
  11. s = s.replace(" ", "");
  12. s.make_ascii_lowercase();
  13. s.chars().filter_map(|c| atbash_cipher_substitute(c)).collect()
  14. }
  15.  
  16.  
  17. /// The same substitution is done both for encode and decode
  18. fn atbash_cipher_substitute(c: char) -> Option<char> {
  19. if c >= 'a' && c <= 'z' {
  20. Some((u32::from('a') + u32::from('z') - u32::from(c)).try_into().unwrap())
  21. } else if c == ',' || c == '.' {
  22. None
  23. } else {
  24. Some(c)
  25. }
  26. }
  27.  
  28. // Helper to add a whitespace every 5 chars
  29. fn add_atbash_whitespace(s: String) -> String {
  30. let mut res = String::new();
  31. for (i, c) in s.chars().enumerate() {
  32. if i != 0 && i%5 == 0 {
  33. res.push(' ');
  34. }
  35. res.push(c);
  36. }
  37. res
  38. }
  39.  
  40.  
  41.  
  42.  
  43. #[cfg(test)]
  44. mod tests {
  45. use super::*;
  46.  
  47. #[test]
  48. fn test_encode_no() {
  49. assert_eq!(
  50. encode(String::from("no")),
  51. "ml"
  52. );
  53. }
  54.  
  55. #[test]
  56. fn test_encode_yes() {
  57. assert_eq!(
  58. encode(String::from("yes")),
  59. "bvh"
  60. );
  61. }
  62.  
  63. #[test]
  64. #[allow(non_snake_case)]
  65. fn test_encode_OMG() {
  66. assert_eq!(
  67. encode(String::from("OMG")),
  68. "lnt"
  69. );
  70. }
  71.  
  72. #[test]
  73. #[allow(non_snake_case)]
  74. fn test_encode_O_M_G() {
  75. assert_eq!(
  76. encode(String::from("O M G")),
  77. "lnt"
  78. );
  79. }
  80.  
  81. #[test]
  82. fn test_encode_long_word() {
  83. assert_eq!(
  84. encode(String::from("mindblowingly")),
  85. "nrmwy oldrm tob"
  86. );
  87. }
  88.  
  89. #[test]
  90. fn test_encode_numbers() {
  91. assert_eq!(
  92. encode(String::from("Testing, 1 2 3, testing.")),
  93. "gvhgr mt123 gvhgr mt"
  94. );
  95. }
  96.  
  97. #[test]
  98. fn test_encode_sentence() {
  99. assert_eq!(
  100. encode(String::from("Truth is fiction.")),
  101. "gifgs rhurx grlm"
  102. );
  103. }
  104.  
  105. #[test]
  106. fn test_encode_all_things() {
  107. let plaintext = String::from("The quick brown fox jumps over the lazy dog.");
  108. let ciphertext = String::from("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
  109. assert_eq!(
  110. encode(plaintext),
  111. ciphertext
  112. );
  113. }
  114.  
  115. #[test]
  116. fn test_decode_word() {
  117. assert_eq!(
  118. decode(String::from("vcvix rhn")),
  119. "exercism"
  120. );
  121. }
  122.  
  123. #[test]
  124. fn test_decode_sentence() {
  125. assert_eq!(
  126. decode(String::from("zmlyh gzxov rhlug vmzhg vkkrm thglm v")),
  127. "anobstacleisoftenasteppingstone"
  128. );
  129. }
  130.  
  131. #[test]
  132. fn test_decode_numbers() {
  133. assert_eq!(
  134. decode(String::from("gvhgr mt123 gvhgr mt")),
  135. "testing123testing"
  136. );
  137. }
  138.  
  139. #[test]
  140. fn test_decode_all_the_letters() {
  141. let ciphertext = String::from("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
  142. let plaintext = String::from("thequickbrownfoxjumpsoverthelazydog");
  143.  
  144. assert_eq!(
  145. decode(ciphertext),
  146. plaintext
  147. );
  148. }
  149.  
  150. #[test]
  151. fn test_decode_with_too_many_spaces() {
  152. assert_eq!(
  153. decode(String::from("vc vix r hn")),
  154. "exercism"
  155. );
  156. }
  157.  
  158. #[test]
  159. fn test_decode_with_no_spaces() {
  160. let ciphertext = String::from("zmlyhgzxovrhlugvmzhgvkkrmthglmv");
  161. let plaintext = String::from("anobstacleisoftenasteppingstone");
  162. assert_eq!(
  163. decode(ciphertext),
  164. plaintext
  165. );
  166. }
  167.  
  168.  
  169. /// additional track specific test
  170. #[test]
  171. fn test_encode_decode() {
  172. assert_eq!(
  173. decode(encode(String::from("Testing, 1 2 3, testing."))),
  174. "testing123testing"
  175. );
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement