-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rot13.rb
82 lines (50 loc) · 1.77 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
=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!"
###solution below is before i had realised there was a very short terminal
application tr;
https://en.wikipedia.org/wiki/ROT13
https://stackoverflow.com/questions/5442436/using-rot13-and-tr-command-for-
having-an-encrypted-email-address
def rot13(message)
result = ""
newHash = Hash.new()
outPut = %w[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z]
inPut = %w[N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
n o p q r s t u v w x y z a b c d e f g h i j k l m]
inPut.zip(outPut).each {|inpt, outpt| newHash[inpt.to_s] = outpt.to_s}
#newHash.each {|k,v| puts "#{k} and #{v}"}
#print newHash
message = message.split("").to_a
#print message
result = ""
for msg in message
newHash.each do |k,v|
if msg == k
result.concat(v)
elsif msg == " "
result.concat(" ")
break
elsif msg
#result.concat(msg)
#puts msg
else
result
end
end
end
result
end
=end
def rot13(string)
string.tr('A-Za-z', 'N-ZA-Mn-za-m')
end
Test.expect(rot13("EBG13 rknzcyr.") == "ROT13 example.")