Skip to content

Instantly share code, notes, and snippets.

@alacret
Created October 15, 2019 02:01
Show Gist options
  • Save alacret/a66b9f7bde45c8cbc1550f726e88bf36 to your computer and use it in GitHub Desktop.
Save alacret/a66b9f7bde45c8cbc1550f726e88bf36 to your computer and use it in GitHub Desktop.
def move_right(x,y):
return x + 1, y
def move_left(x,y):
return x - 1,y
def move_up(x,y):
return x, y - 1
def move_down(x,y):
return x, y + 1
R, L, U, D = 1,2,3,4
def create_spiral(n):
if type(n) != int:
return []
if n < 1:
return []
matrix = []
for i in range(n):
_l = []
for j in range(n):
_l.append(-1)
matrix.append(_l)
acc = 1
turns = 0
x, y = 0, 0
matrix[y][x] = acc
print(x,y)
steps_x, steps_y = n - 1, n -1
direction = R
while True:
if steps_x == 0 and steps_y == 0:
break
if turns == 3:
print('Dicrease:', turns)
steps_x = steps_x - 1
steps_y = steps_y - 1
if turns >= 4 and (turns + 3) % 2 == 0:
print('Dicrease:', turns)
steps_x = steps_x - 1
steps_y = steps_y - 1
if direction == R:
for i in range(steps_x):
x,y = move_right(x,y)
acc = acc + 1
matrix[y][x] = acc
print('R',x,y,'T:', acc)
direction = D
turns = turns + 1
continue
if direction == D:
for i in range(steps_y):
x,y = move_down(x,y)
acc = acc + 1
matrix[y][x] = acc
print('D',x,y,'T:', turns)
direction = L
turns = turns + 1
continue
if direction == L:
for i in range(steps_x):
x,y = move_left(x,y)
acc = acc + 1
matrix[y][x] = acc
print('L', x,y,'T:', turns)
direction = U
turns = turns + 1
continue
if direction == U:
for i in range(steps_y):
x,y = move_up(x,y)
acc = acc + 1
matrix[y][x] = acc
print('U',x,y,'T:', turns)
direction = R
turns = turns + 1
continue
print(matrix)
return matrix
"""
1(0,0) 2(1,0) 3(2,0) 4(3,0)
12(0,1) 13(1,1) 14(2,1) 5(3,1)
11(0,2) 16(1,2) 15(2,2) 6(3,2)
10(0,3) 9(1,3) 8(2,3) 7(3,3)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment