Created
March 2, 2016 16:09
-
-
Save lukewduncan/6c6c35c0e03560f2976d to your computer and use it in GitHub Desktop.
Technical Question
This file contains hidden or 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 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