Skip to content

Instantly share code, notes, and snippets.

@lukewduncan
Created March 2, 2016 16:09
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 lukewduncan/6c6c35c0e03560f2976d to your computer and use it in GitHub Desktop.
Save lukewduncan/6c6c35c0e03560f2976d to your computer and use it in GitHub Desktop.
Technical Question
def rotx(x, string, encrypt=true)
alphabet = Array("A".."Z") + Array("a".."z")
results = []
if encrypt == true
key_encrypt_true = Hash[alphabet.zip(alphabet.rotate(x))]
string.chars.each do |i|
if ('a'..'z').include? i
results << key_encrypt_true.fetch(i).downcase
elsif ('A'..'Z').include? i
results << key_encrypt_true.fetch(i).upcase
else
results << i
end
end
return results.join
else
key_encrypt_false = Hash[alphabet.zip(alphabet.rotate(26 - x))]
string.chars.each do |i|
if ('a'..'z').include? i
results << key_encrypt_false.fetch(i).downcase
elsif ('A'..'Z').include? i
results << key_encrypt_false.fetch(i).upcase
else
results << i
end
end
return results.join
end
end
raise "You shall not pass!" if not rotx(10, "Hello, World") == "Rovvy, Gybvn"
raise "You shall not pass!" if not rotx(10, "Rovvy, Gybvn", false) == "Hello, World"
raise "You shall not pass!" if not rotx(36, "Hello, World") == "Rovvy, Gybvn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment