Advertisement
Guest User

first teenagersprogramming weekly challenge submission

a guest
Mar 23rd, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.57 KB | None | 0 0
  1. # Challenge week 1
  2. # 3/23/15
  3. # Meshiest / _cake
  4.  
  5. # Test data:
  6. # psghactuxjnmrbyzefklwvoidq
  7. # uammy oyfmh!
  8.  
  9. puts "Enter cipher: "
  10. cipher = gets.chomp
  11.  
  12. unless /((?!=\G)([a-z])){26}/i =~ cipher
  13.     puts "Invalid cipher, requires 26 unique letters"
  14.     exit
  15. end
  16.  
  17. map = Hash[*((cipher.chars.zip ('a'..'z').to_a).flatten)] # Map alphabet to each cipher character
  18.  
  19. puts "Enter ciphertext: "
  20. text = gets.chomp
  21.  
  22. text = text.downcase
  23.     .chars
  24.     .map{ |c|
  25.         map[c] || c # get the alternate character, otherwise, the same character
  26.     } * ''
  27.  
  28. puts "Deciphered text: "
  29. puts text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement