forked from frankobe/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zigzag_conversion_.py
29 lines (27 loc) · 934 Bytes
/
zigzag_conversion_.py
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
class Solution:
# @return a string
def convert(self, s, nRows):
result = ''
if nRows == 1:
return s
interval = 2 * (nRows - 1)
times, remainder = divmod(len(s), interval)
for i in range(nRows):
if i == 0 or i == nRows - 1:
for j in range(times):
result += s[i+interval*j]
if i <= remainder-1:
result += s[i+interval*times]
else:
for j in range(times):
result += s[i+interval*j]
result += s[interval-i+interval*j]
if i<=remainder-1:
result += s[i+interval*times]
if interval-i<=remainder-1:
result += s[interval-i+interval*times]
return result
if __name__ == '__main__':
test = Solution()
print test.convert('ABC', 3)
raw_input()