-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzigzag.py
160 lines (118 loc) · 3.9 KB
/
zigzag.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import numpy as np
def zigzag(input):
# initializing the variables
# ----------------------------------
h = 0
v = 0
vmin = 0
hmin = 0
vmax = input.shape[0]
hmax = input.shape[1]
# print(vmax ,hmax )
i = 0
output = np.zeros((vmax * hmax))
# ----------------------------------
while ((v < vmax) and (h < hmax)):
if ((h + v) % 2) == 0: # going up
if (v == vmin):
# print(1)
output[i] = input[v, h] # if we got to the first line
if (h == hmax):
v = v + 1
else:
h = h + 1
i = i + 1
elif ((h == hmax - 1) and (v < vmax)): # if we got to the last column
# print(2)
output[i] = input[v, h]
v = v + 1
i = i + 1
elif ((v > vmin) and (h < hmax - 1)): # all other cases
# print(3)
output[i] = input[v, h]
v = v - 1
h = h + 1
i = i + 1
else: # going down
if ((v == vmax - 1) and (h <= hmax - 1)): # if we got to the last line
# print(4)
output[i] = input[v, h]
h = h + 1
i = i + 1
elif (h == hmin): # if we got to the first column
# print(5)
output[i] = input[v, h]
if (v == vmax - 1):
h = h + 1
else:
v = v + 1
i = i + 1
elif ((v < vmax - 1) and (h > hmin)): # all other cases
# print(6)
output[i] = input[v, h]
v = v + 1
h = h - 1
i = i + 1
if ((v == vmax - 1) and (h == hmax - 1)): # bottom right element
# print(7)
output[i] = input[v, h]
break
# print ('v:',v,', h:',h,', i:',i)
return output
def inverseZigzag(input, vmax, hmax):
# print input.shape
# initializing the variables
# ----------------------------------
h = 0
v = 0
vmin = 0
hmin = 0
output = np.zeros((vmax, hmax))
i = 0
# ----------------------------------
while ((v < vmax) and (h < hmax)):
# print ('v:',v,', h:',h,', i:',i)
if ((h + v) % 2) == 0: # going up
if (v == vmin):
# print(1)
output[v, h] = input[i] # if we got to the first line
if (h == hmax):
v = v + 1
else:
h = h + 1
i = i + 1
elif ((h == hmax - 1) and (v < vmax)): # if we got to the last column
# print(2)
output[v, h] = input[i]
v = v + 1
i = i + 1
elif ((v > vmin) and (h < hmax - 1)): # all other cases
# print(3)
output[v, h] = input[i]
v = v - 1
h = h + 1
i = i + 1
else: # going down
if ((v == vmax - 1) and (h <= hmax - 1)): # if we got to the last line
# print(4)
output[v, h] = input[i]
h = h + 1
i = i + 1
elif (h == hmin): # if we got to the first column
# print(5)
output[v, h] = input[i]
if (v == vmax - 1):
h = h + 1
else:
v = v + 1
i = i + 1
elif ((v < vmax - 1) and (h > hmin)): # all other cases
output[v, h] = input[i]
v = v + 1
h = h - 1
i = i + 1
if ((v == vmax - 1) and (h == hmax - 1)): # bottom right element
# print(7)
output[v, h] = input[i]
break
return output