-
Notifications
You must be signed in to change notification settings - Fork 68
/
ROT13.rb
41 lines (34 loc) · 1.1 KB
/
ROT13.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
=begin
How can you tell an extrovert from an introvert at NSA? Va gur ryringbef, gur
rkgebireg ybbxf ng gur BGURE thl'f fubrf.
I found this joke on USENET, but the punchline is scrambled. Maybe you can
decipher it? According to Wikipedia, ROT13 (http://en.wikipedia.org/wiki/ROT13)
is frequently used to obfuscate jokes on USENET.
Hint: For this task you're only supposed to substitue characters. Not spaces,
punctuation, numbers etc. Test examples:
rot13("EBG13 rknzcyr.") == "ROT13 example.");
rot13("This is my first ROT13 excercise!") = "Guvf vf zl svefg EBG13 rkprepvfr!")
=end
# My Solution
def rot13(message)
result = ""
lower = ("abcdefghijklmnopqrstuvwxyz").split("").rotate(-13)
upper = ("ABCDEFGHIJKLMNOPQRSTUVWXWZ").split("").rotate(-13)
message.split("").each do |x|
num = x.ord
letter = ""
if num > 64 && num < 91
num -= 65
letter = upper[num]
elsif num > 96 && num < 123
num -= 97
letter = lower[num]
end
letter == "" ? result += x : result += letter
end
result
end
# Better Solution
def rot13(message)
message.tr('a-zA-Z', 'n-za-mN-ZA-M')
end