Created
April 15, 2016 14:47
-
-
Save constantine-nikolaou/fa744b3fd11945671bb4720d7e308194 to your computer and use it in GitHub Desktop.
Solution to Caesar Cipher implemented 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 alphabet | |
Array('a'..'z') | (Array('A'..'Z')) | |
end | |
def cs_encrypt(string, key = 0) | |
# Rotate switches alphabets based on the passed key | |
dictionary = Hash[alphabet.zip(alphabet.rotate(key))] | |
string.chars.map { |str| dictionary.fetch(str, ' ') } | |
end | |
string = cs_encrypt("onCe Upon a tIme there was a little house On the TOP of the hill", 3).join | |
puts string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment