Skip to content

Instantly share code, notes, and snippets.

@junqueira
Last active October 7, 2016 11:19
Show Gist options
  • Save junqueira/b2f97a8c70fce141d50756f49dad1361 to your computer and use it in GitHub Desktop.
Save junqueira/b2f97a8c70fce141d50756f49dad1361 to your computer and use it in GitHub Desktop.
# https://www.hackerrank.com/challenges/ruby-enumerable-collect
def rot(string, num)
if num > 25
num = num - (25 *(num/25))
end
new_UpFirst = (65 + num).chr
new_UpLast = (65 + num - 1).chr
new_DwnFirst = (97 + num).chr
new_DwnLast = (97 + num - 1).chr
case num
when 0
puts string
when 1
puts string.tr "A-Za-z", "#{new_UpFirst}-ZA#{new_DwnFirst}-za"
when 25
puts string.tr "A-Za-z", "ZA-#{new_UpLast}za-#{new_DwnLast}"
else
puts string.tr "A-Za-z", "#{new_UpFirst}-ZA-#{new_UpLast}#{new_DwnFirst}-za-#{new_DwnLast}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment