-
-
Save ncaron/04cf9a1c011cff3df2b3ce853adbd62d to your computer and use it in GitHub Desktop.
Caesar Cipher solution from The Odin Project
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(text, shift) | |
uppercase_bound = 65 | |
lowercase_bound = 97 | |
number_of_letters = 26 | |
secret = text.chars.map do |char| | |
if char =~ /[A-Z]/ | |
char = ((char.ord - uppercase_bound + shift) % number_of_letters + uppercase_bound).chr | |
elsif char =~ /[a-z]/ | |
char = ((char.ord - lowercase_bound + shift) % number_of_letters + lowercase_bound).chr | |
end | |
char | |
end.join("") | |
end | |
puts caesar_cipher("What a string!", 5) == "Bmfy f xywnsl!" | |
puts caesar_cipher("Hey, since you're reading this, please check my postcard collection website at https://ncaron.github.io/world-through-postcards/", 17) == "Yvp, jzetv pfl'iv ivruzex kyzj, gcvrjv tyvtb dp gfjktriu tfccvtkzfe nvsjzkv rk ykkgj://etrife.xzkyls.zf/nficu-kyiflxy-gfjktriuj/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment