Skip to content

Instantly share code, notes, and snippets.

@ncaron
Created January 13, 2018 05:38
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 ncaron/04cf9a1c011cff3df2b3ce853adbd62d to your computer and use it in GitHub Desktop.
Save ncaron/04cf9a1c011cff3df2b3ce853adbd62d to your computer and use it in GitHub Desktop.
Caesar Cipher solution from The Odin Project
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