-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathZigZag_Conversion.py
More file actions
36 lines (30 loc) · 808 Bytes
/
ZigZag_Conversion.py
File metadata and controls
36 lines (30 loc) · 808 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
"""
@author - Anirudh Sharma
"""
def convert(s: str, numRows: int) -> str:
# Base condition
if s is None and numRows <= 0:
return ""
if numRows == 1:
return s
# Resultant string
result = ""
# Step size
step = 2 * numRows - 2
# Loop for each row
for i in range(0, numRows):
# Loop for each character in the row
for j in range(i, len(s), step):
result += s[j]
if i != 0 and i != numRows - 1 and (j + step - 2 * i) < len(s):
result += s[j + step - 2 * i]
return result
if __name__ == '__main__':
s = "PAYPALISHIRING"
numRows = 3
print(convert(s, numRows))
numRows = 4
print(convert(s, numRows))
s = "ANIRUDHSHARMAISGREAT"
numRows = 5
print(convert(s, numRows))