-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag.py
More file actions
50 lines (39 loc) · 1.12 KB
/
zigzag.py
File metadata and controls
50 lines (39 loc) · 1.12 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
44
45
46
47
48
49
50
"""
Task:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given
number of rows like this:
P I N
A L S I G
Y A H R
P I
And then read line by line: ""PINALSIGYAHRPI. Write the code that will take
a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
>>> zigzag_convert("PAYPALISHIRING", 3)
'PAHNAPLSIIGYIR'
>>> zigzag_convert("PAYPALISHIRING", 4)
'PINALSIGYAHRPI'
>>> zigzag_convert("", 1)
''
>>> zigzag_convert("ab", 1)
'ab'
"""
def zigzag_convert(s, rows):
# corner cases
if len(s) <= rows or rows == 1:
return s
# set up initial state
row = 0
inc = 1
result = ["" for _ in range(rows)]
# put every char in order
for char in s:
result[row] += char # not efficient, but simple to write
row += inc
if row == 0 or row == rows - 1:
inc *= -1 # reverse increment direction
return "".join(result)
if __name__ == '__main__':
import doctest
doctest.testmod()