Created
June 19, 2015 23:55
-
-
Save alexeyzab/d99b6a72889aa2743cad to your computer and use it in GitHub Desktop.
Caesar cipher in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def caesar_cipher(input, shift) | |
positions = input.unpack("C*") | |
shifted_positions = positions.map do |pos| | |
case pos | |
when (65..90), (97..122) | |
shifted = pos + (shift % 26) | |
if (shifted > 90 && shifted < 97) || (shifted > 122) | |
shifted = shifted - 26 | |
end | |
pos = shifted | |
else | |
pos | |
end | |
end | |
shifted_positions.pack("C*") | |
end | |
puts caesar_cipher("Fraq hf gur pbqr lbh hfrq gb qrpbqr guvf zrffntr", 13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment