- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0006-zigzag-conversion.rb
40 lines (32 loc) · 876 Bytes
/
0006-zigzag-conversion.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
# frozen_string_literal: true
# 6. Verifying an Alien Dictionary
# https://leetcode.com/problems/zigzag-conversion
# @param {String} s
# @param {Integer} num_rows
# @return {String}
defconvert(s,num_rows)
returnsifs.size == 1 || num_rows == 1
str=s.chars
res=Array.new(num_rows){[]}
j=0
whilestr.any?
ifnum_rows == 1 || (j % (num_rows - 1)).zero?
num_rows.times{ |i| res[i][j]=str.shift}
else
res[num_rows - (j % (num_rows - 1)) - 1][j]=str.shift
end
j += 1
end
res.flatten.compact.join
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_convert < Test::Unit::TestCase
deftest_
assert_equal"PAHNAPLSIIGYIR",convert("PAYPALISHIRING",3)
assert_equal"PINALSIGYAHRPI",convert("PAYPALISHIRING",4)
assert_equal"A",convert("A",1)
end
end