-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZag_Conversion.py
More file actions
37 lines (31 loc) · 965 Bytes
/
ZigZag_Conversion.py
File metadata and controls
37 lines (31 loc) · 965 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
# The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this:
# (you may want to display this pattern in a fixed font for better legibility)
# https://leetcode.com/problems/zigzag-conversion/
class Solution(object):
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
solution = {}
line = 1
step = 1
if numRows < 2:
return s
for letter in s:
if line not in solution:
solution[line] = letter
else:
solution[line] += letter
line += step
if line == numRows or line == 1:
step *= -1
final_string = ''.join(solution.values())
return(final_string)
def main():
solution = Solution()
input = "PAYPALISHIRING"
print(solution.convert(input, 3))
if __name__ == "__main__":
main()