Last active
March 6, 2020 13:33
-
-
Save novasdream/9061d0536a148b1ce77a27438c2bce19 to your computer and use it in GitHub Desktop.
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(message, key) | |
alphabet = [*"a".."z"] | |
alphabet_cifred = alphabet[key..26] + alphabet[0..key] | |
c_array = message.split('') | |
c_lambda = lambda { | |
|c| | |
upper = c == c.upcase | |
if alphabet.index(c.downcase) | |
character = alphabet_cifred[alphabet.index(c.downcase)] | |
upper ? character.upcase : character | |
else | |
c | |
end | |
} | |
c_array.map(&c_lambda).join('') | |
end | |
puts caesar_cipher("What a string!", 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment