-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path0006-zigzag-conversion.rb
More file actions
40 lines (32 loc) · 876 Bytes
/
0006-zigzag-conversion.rb
File metadata and controls
40 lines (32 loc) · 876 Bytes
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}
def convert(s, num_rows)
return s if s.size == 1 || num_rows == 1
str = s.chars
res = Array.new(num_rows) { [] }
j = 0
while str.any?
if num_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"
class Test_convert < Test::Unit::TestCase
def test_
assert_equal "PAHNAPLSIIGYIR", convert("PAYPALISHIRING", 3)
assert_equal "PINALSIGYAHRPI", convert("PAYPALISHIRING", 4)
assert_equal "A", convert("A", 1)
end
end