Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::convert::TryInto;
- pub fn encode(mut s: String) -> String {
- s = s.replace(" ", "");
- s.make_ascii_lowercase();
- s = s.chars().filter_map(|c| atbash_cipher_substitute(c)).collect();
- add_atbash_whitespace(s)
- }
- pub fn decode(mut s: String) -> String {
- s = s.replace(" ", "");
- s.make_ascii_lowercase();
- s.chars().filter_map(|c| atbash_cipher_substitute(c)).collect()
- }
- /// The same substitution is done both for encode and decode
- fn atbash_cipher_substitute(c: char) -> Option<char> {
- if c >= 'a' && c <= 'z' {
- Some((u32::from('a') + u32::from('z') - u32::from(c)).try_into().unwrap())
- } else if c == ',' || c == '.' {
- None
- } else {
- Some(c)
- }
- }
- // Helper to add a whitespace every 5 chars
- fn add_atbash_whitespace(s: String) -> String {
- let mut res = String::new();
- for (i, c) in s.chars().enumerate() {
- if i != 0 && i%5 == 0 {
- res.push(' ');
- }
- res.push(c);
- }
- res
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_encode_no() {
- assert_eq!(
- encode(String::from("no")),
- "ml"
- );
- }
- #[test]
- fn test_encode_yes() {
- assert_eq!(
- encode(String::from("yes")),
- "bvh"
- );
- }
- #[test]
- #[allow(non_snake_case)]
- fn test_encode_OMG() {
- assert_eq!(
- encode(String::from("OMG")),
- "lnt"
- );
- }
- #[test]
- #[allow(non_snake_case)]
- fn test_encode_O_M_G() {
- assert_eq!(
- encode(String::from("O M G")),
- "lnt"
- );
- }
- #[test]
- fn test_encode_long_word() {
- assert_eq!(
- encode(String::from("mindblowingly")),
- "nrmwy oldrm tob"
- );
- }
- #[test]
- fn test_encode_numbers() {
- assert_eq!(
- encode(String::from("Testing, 1 2 3, testing.")),
- "gvhgr mt123 gvhgr mt"
- );
- }
- #[test]
- fn test_encode_sentence() {
- assert_eq!(
- encode(String::from("Truth is fiction.")),
- "gifgs rhurx grlm"
- );
- }
- #[test]
- fn test_encode_all_things() {
- let plaintext = String::from("The quick brown fox jumps over the lazy dog.");
- let ciphertext = String::from("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
- assert_eq!(
- encode(plaintext),
- ciphertext
- );
- }
- #[test]
- fn test_decode_word() {
- assert_eq!(
- decode(String::from("vcvix rhn")),
- "exercism"
- );
- }
- #[test]
- fn test_decode_sentence() {
- assert_eq!(
- decode(String::from("zmlyh gzxov rhlug vmzhg vkkrm thglm v")),
- "anobstacleisoftenasteppingstone"
- );
- }
- #[test]
- fn test_decode_numbers() {
- assert_eq!(
- decode(String::from("gvhgr mt123 gvhgr mt")),
- "testing123testing"
- );
- }
- #[test]
- fn test_decode_all_the_letters() {
- let ciphertext = String::from("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
- let plaintext = String::from("thequickbrownfoxjumpsoverthelazydog");
- assert_eq!(
- decode(ciphertext),
- plaintext
- );
- }
- #[test]
- fn test_decode_with_too_many_spaces() {
- assert_eq!(
- decode(String::from("vc vix r hn")),
- "exercism"
- );
- }
- #[test]
- fn test_decode_with_no_spaces() {
- let ciphertext = String::from("zmlyhgzxovrhlugvmzhgvkkrmthglmv");
- let plaintext = String::from("anobstacleisoftenasteppingstone");
- assert_eq!(
- decode(ciphertext),
- plaintext
- );
- }
- /// additional track specific test
- #[test]
- fn test_encode_decode() {
- assert_eq!(
- decode(encode(String::from("Testing, 1 2 3, testing."))),
- "testing123testing"
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement