Created
May 21, 2014 22:23
-
-
Save aprilrabkin/3af125598e08b6ecdc8b to your computer and use it in GitHub Desktop.
Caesar Cipher Before Refactoring
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
$lower_case = "abcdefghijklmnopqrstuvwxyz" | |
$upper_case = $lower_case.upcase | |
def caesar_encode(string, offset) | |
array = string.split("") | |
array.map! do |l| | |
if /[a-z]/ === l | |
idx = $lower_case.rindex(l) | |
idx = (idx + offset) % 26 | |
$lower_case[idx] | |
elsif /[A-Z]/ === l | |
idx = $upper_case.rindex(l) | |
idx = (idx + offset) % 26 | |
$upper_case[idx] | |
else | |
l | |
end | |
end | |
array.join | |
end | |
def caesar_decode(string, offset) | |
array = string.split("") | |
array.map! do |l| | |
if /[a-z]/ === l | |
idx = $lower_case.rindex(l) | |
idx = (idx - offset) % 26 | |
$lower_case[idx] | |
elsif /[A-Z]/ === l | |
idx = $upper_case.rindex(l) | |
idx = (idx - offset) % 26 | |
$upper_case[idx] | |
else | |
l | |
end | |
end | |
array.join | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment