-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_code
executable file
·38 lines (32 loc) · 1.23 KB
/
secret_code
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
#!/usr/bin/env ruby
LETTERS_1 = ("a".."z").to_a
LETTERS_SYMBOLS = (0.chr..255.chr).to_a.grep(/[[:print:]]/) - LETTERS_1 - LETTERS_1.map(&:upcase) - [' ', '\\', "'"]
def secret_code(encoding, direction, text)
case encoding
when "1-letter"
case direction
when "encode"
# you think forward 1 letter for each letter
text.split('').map { |c| LETTERS_1[LETTERS_1.index(c) + 1] }.join
when "decode"
# you think backward 1 letter for each letter
text.split('').map { |c| LETTERS_1[LETTERS_1.index(c) - 1] }.join
else
raise "we dont support '#{direction}', but we support 'encode' and 'decode'"
end
when "symbol"
case direction
when "encode"
# you think backward 26 letter for each letter
text.split('').map { |c| c == ' ' ? c : LETTERS_SYMBOLS[LETTERS_1.index(c)] }.join
when "decode"
# you think forward 26 letter for each letter
text.split('').map { |c| c == ' ' ? c : LETTERS_1[LETTERS_SYMBOLS.index(c)] }.join
else
raise "we dont support '#{direction}', but we support 'encode' and 'decode'"
end
else
raise "unsupported encoding '#{encoding}', we support only '1-letter' and 'symbol'"
end
end
puts secret_code(*ARGV) unless $0.include?("_test.rb")