-
Notifications
You must be signed in to change notification settings - Fork 1
/
006 ZigZag Conversion.py
125 lines (121 loc) · 4.11 KB
/
006 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
def count(s):
count = 0
for i in s:
count = count + 1
return (count)
m=count(s)
if m==0:
return('')
if m==1:
return(s)
n=numRows
if n==1:
return(s)
if m<=n:
return(s)
a=int((m-n)/(n-1)) #6
b=m-((a+1)*n-a) #1
last=''
if a%2==1:
c=(a+1)/2
#print(c)
c=int(c)
i=1
while i<=n: #i从1-n
if i==1:
print(s[1-1])
last = last + s[1- 1]
j=1
while j <=c: #j从0到c=3
print(s[1+(2*n-2)*j-1])
last=last+s[1+(2*n-2)*j-1]
j=j+1
if i>=2 and i<=1+b:
print(s[i - 1])
print(i)
last = last + s[i - 1]
j=1
while j<=c:
print(s[i+(2*n-2)*(j-1)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(j-1)+(2*n-2*i)-1]
print(s[i+(2*n-2)*(j)-1])
last= last + s[i+(2*n-2)*(j)-1]
j=j+1
if i>1+b and i<=n-1:
print(s[i - 1])
last = last + s[i - 1]
j=1
while j<=c-1:
print(s[i+(2*n-2)*(j-1)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(j-1)+(2*n-2*i)-1]
print(s[i+(2*n-2)*(j)-1])
last= last + s[i+(2*n-2)*(j)-1]
j=j+1
print(s[i+(2*n-2)*(c-1)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(c-1)+(2*n-2*i)-1]
if i==n:
print(s[n-1])
last = last + s[n- 1]
j=1
while j <=c-1: #j从0到c=2
print(s[n+(2*n-2)*j-1])
last=last+s[n+(2*n-2)*j-1]
j=j+1
i=i+1
if a%2==0:
c=(a)/2
print(c)
c=int(c)
i=1
while i<=n: #i从1-n
if i==1:
print(s[1-1])
last = last + s[1- 1]
j=1
while j <=c: #j从0到c=3
print(s[1+(2*n-2)*j-1])
last=last+s[1+(2*n-2)*j-1]
j=j+1
if i>=2 and i<n-b:
print(s[i - 1])
last = last + s[i - 1]
j=1
while j<=c:
print(s[i+(2*n-2)*(j-1)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(j-1)+(2*n-2*i)-1]
print(s[i+(2*n-2)*(j)-1])
last= last + s[i+(2*n-2)*(j)-1]
j=j+1
if i>=n-b and i<=n-1:
print(s[i - 1])
last = last + s[i - 1]
j=1
while j<=c:
print(s[i+(2*n-2)*(j-1)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(j-1)+(2*n-2*i)-1]
print(s[i+(2*n-2)*(j)-1])
last= last + s[i+(2*n-2)*(j)-1]
j=j+1
print(i+(2*n-2)*(c)+(2*n-2*i)-1)
print(s[i+(2*n-2)*(c)+(2*n-2*i)-1])
last = last + s[i+(2*n-2)*(c)+(2*n-2*i)-1]
if i==n:
print(s[n-1])
last = last + s[n- 1]
j=1
while j <=c: #j从0到c=1
print(s[n+(2*n-2)*j-1])
last=last+s[n+(2*n-2)*j-1]
j=j+1
i=i+1
return(last)
a=Solution()
B=a.convert("PAYPALIS", 3)
print(B)