zigzag conversion
1from collections import defaultdict as dd
2
3class Solution(object):
4 def convert(self, s, numRows):
5 x, y, xi, yi, i = 0, 0, 0, 0, 0
6 o = {i:dd(list) for i in range(numRows)}
7 while i < len(s):
8 top = i % ((numRows-1) * 2) == 0
9 bottom = i % (numRows-1) == 0
10 if top:
11 x, y = 0, 1
12 elif bottom:
13 x, y = 1, -1
14 o[yi][xi] = s[i]
15 xi, yi, i = xi+x, yi+y, i+1
16 return ''.join([''.join(x.values()) for x in o.values()])
17
18print(Solution().convert("PAYPALISHIRING", 3))