-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.py
More file actions
43 lines (36 loc) · 1.16 KB
/
zigzag_conversion.py
File metadata and controls
43 lines (36 loc) · 1.16 KB
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
#!/usr/bin/env python3
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows == 1 or len(s) <= numRows:
return s
rows = ['' for _ in range(numRows)]
index, step = 0, 1
for char in s:
rows[index] += char
if index == 0:
step = 1
elif index == numRows - 1:
step = -1
index += step
return ''.join(rows)
solution = Solution()
input1 = "PAYPALISHIRING"
output1 = solution.convert(input1, 3)
expected1 = "PAHNAPLSIIGYIR"
print(f"Input: {input1}, Output: {output1}, Expected: {expected1}")
assert output1 == expected1
input2 = "HELLO"
output2 = solution.convert(input2, 1)
expected2 = "HELLO"
print(f"Input: {input2}, Output: {output2}, Expected: {expected2}")
assert output2 == expected2
input3 = "ABCD"
output3 = solution.convert(input3, 2)
expected3 = "ACBD"
print(f"Input: {input3}, Output: {output3}, Expected: {expected3}")
assert output3 == expected3
input4 = ""
output4 = solution.convert(input4, 5)
expected4 = ""
print(f"Input: {input4}, Output: {output4}, Expected: {expected4}")
assert output4 == expected4