Skip to content

Instantly share code, notes, and snippets.

@novasdream
Last active March 6, 2020 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novasdream/9061d0536a148b1ce77a27438c2bce19 to your computer and use it in GitHub Desktop.
Save novasdream/9061d0536a148b1ce77a27438c2bce19 to your computer and use it in GitHub Desktop.
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