daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let input = "MEET AT DAWN";
  3.     let key = "ALLY";
  4.     println!("{}", cipher(input, key))
  5. }
  6.  
  7. fn cipher(input: &str, key: &str) -> String {
  8.     let mut key = key.chars().cycle();
  9.     input
  10.         .chars()
  11.         .map(|c| if c.is_whitespace() { c } else { key.next().unwrap() })
  12.         .collect()
  13. }
  14.  
  15. fn cipher_fold(input: &str, key: &str) -> String {
  16.     input.chars().fold((String::new(), 0), |(mut acc, i), c| {
  17.         if c.is_whitespace() {
  18.             acc.push(c);
  19.                 (acc, i)
  20.         } else {
  21.             let k = key.chars().nth(i).unwrap();
  22.             acc.push(k);
  23.             (acc, (i + 1) % key.len())
  24.         }
  25.     }).0
  26. }
  27.  
  28. #[test]
  29. fn test_cipher() {
  30.     let input = "MEET AT DAWN";
  31.     let key = "ALLY";
  32.     let expected = "ALLY AL LYAL";
  33.     assert_eq!(cipher(input, key), expected);
  34.     assert_eq!(cipher_fold(input, key), expected);
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top