Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.73 KB | None | 0 0
  1. def read_lines
  2.   i = gets.split(' ').map(&:to_i)
  3.   array = []
  4.   1.upto i[0] do
  5.     line = gets.chomp.split(' ')
  6.     array.push(line)
  7.   end
  8.   return array, i
  9. end
  10.  
  11. def rotate_alphabet(n)
  12.   alphabet = Array('A'..'Z')
  13.   encrypted_alphabet = Hash[alphabet.zip(alphabet.rotate(n[1]))]
  14. end
  15.  
  16. def caesar_shift_cipher(alphabet, lines)
  17.   alphabet = alphabet.invert
  18.   answer = []
  19.   lines = lines.map{ |x| x }
  20.   lines.each do |line|
  21.     line = line.to_s
  22.     crypted = line.chars.map { |a| alphabet.fetch(a, ' ') }
  23.     crypted = crypted.reject {|x| x.empty?}
  24.     answer.push(crypted)
  25.     end
  26.   answer
  27. end
  28.  
  29.  
  30. line = read_lines
  31. encryptor = rotate_alphabet(line[1])
  32. answer = caesar_shift_cipher(encryptor, line[0])
  33. puts answer.join('')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement