File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def zigzag (block ):
4
+ # move the point to different directions
5
+ up = lambda point : (point [0 ] - 1 , point [1 ])
6
+ down = lambda point : (point [0 ] + 1 , point [1 ])
7
+ right = lambda point : (point [0 ], point [1 ] + 1 )
8
+ left = lambda point : (point [0 ], point [1 ] - 1 )
9
+
10
+ up_right = lambda point : up (right (point ))
11
+ down_left = lambda point : down (left (point ))
12
+
13
+ rows , cols = block .shape
14
+
15
+ # return true if point is inside the block bounds
16
+ def inbounds (point ):
17
+ return 0 <= point [0 ] < rows and 0 <= point [1 ] < cols
18
+
19
+ # start in the top-left cell
20
+ point = (0 , 0 )
21
+
22
+ # TODO: rename this variable later
23
+ even = True
24
+
25
+ arr = np .empty ((rows * cols , 1 ), np .int16 );
26
+ for i in range (rows * cols ):
27
+ arr [i ] = block [point ]
28
+ if even :
29
+ if inbounds (up_right (point )):
30
+ point = up_right (point )
31
+ else :
32
+ even = False
33
+ point = right (point ) if inbounds (right (point )) else down (point )
34
+ else :
35
+ if inbounds (down_left (point )):
36
+ point = down_left (point )
37
+ else :
38
+ even = True
39
+ point = down (point ) if inbounds (down (point )) else right (point )
40
+ return arr
You can’t perform that action at this time.
0 commit comments