-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathatbash_cipher.rb
58 lines (50 loc) · 1.33 KB
/
atbash_cipher.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
# Problem: https://exercism.org/tracks/ruby/exercises/atbash-cipher
# Solution
class Atbash
LETTERS = ("a".."z").to_a
def self.encode(plaintext)
return "" if plaintext == ""
ciphertext = ""
plaintext = plaintext.downcase
curr_cipher_len = 0
for i in 0...(plaintext.length)
if curr_cipher_len==5
ciphertext+=" "
curr_cipher_len = 0
end
if plaintext[i].match(/\d+/)
ciphertext+=plaintext[i]
curr_cipher_len+=1
next
end
unless LETTERS.index(plaintext[i]).nil?
ciphertext+= LETTERS[25-LETTERS.index(plaintext[i])]
curr_cipher_len+=1
end
end
ciphertext.strip
end
def self.decode(ciphertext)
return "" if ciphertext == ""
plaintext = ""
ciphertext = ciphertext.gsub(" ","")
ciphertext.chars.each do |char|
if LETTERS.index(char).nil?
plaintext+=char
else
plaintext+= LETTERS[25-LETTERS.index(char)]
end
end
plaintext
end
end
# Solution(idiomatic ruby)
lass Atbash
ALPHA = ('a'..'z').to_a.join
def self.encode(text)
decode(text).chars.each_slice(5).map(&:join).join(' ')
end
def self.decode(text)
text.downcase.gsub(/\W/, '').tr(ALPHA, ALPHA.reverse)
end
end