-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathcrypto_square.rb
42 lines (38 loc) · 1.15 KB
/
crypto_square.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
# Problem: https://exercism.org/tracks/ruby/exercises/crypto-square
# Solution
class Crypto
def initialize(plaintext)
@plaintext = plaintext
end
def ciphertext
normalized_text = @plaintext.downcase.gsub(/\W/,"")
text_len = normalized_text.length
return "" if text_len==0
col_len = Math.sqrt(text_len).ceil
row_len = col_len*(col_len-1) < text_len ? col_len : col_len-1
char_deficit = (col_len*row_len)-text_len
normalized_text+=(" "*char_deficit)
normalized_text
.chars
.each_slice(col_len)
.to_a
.transpose
.map{|col| col.join("")}
.join(" ")
end
end
# Solution (idiomatic)
class Crypto
def initialize(plaintext)
@plaintext = plaintext
end
def ciphertext
normalized_text = @plaintext.downcase.gsub(/[^\da-z]/, '')
length = normalized_text.length
return '' if length.zero?
cols = Math.sqrt(length).ceil
rows = length.quo(cols).ceil
rectangle_base = normalized_text + ' ' * (cols * rows - length)
rectangle_base.chars.each_slice(cols).to_a.transpose.map(&:join).join(' ')
end
end