Skip to content

Commit dc86e33

Browse files
committed
Update spiral_matrix.py
1 parent 698fe59 commit dc86e33

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

spiral-matrix/spiral_matrix.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,13 @@ def _set_direction(direction: str,
6161
leaving bounds or revisiting filled cells.
6262
"""
6363
row, col = x_y
64-
if direction == "right":
65-
if col + 1 < size and result[row][col + 1] == 0:
66-
direction = "right"
67-
else:
68-
direction = "down"
69-
elif direction == "down":
70-
if row + 1 < size and result[row + 1][col] == 0:
71-
direction = "down"
72-
else:
73-
direction = "left"
74-
elif direction == "up":
75-
if row - 1 >= 0 and result[row - 1][col] == 0:
76-
direction = "up"
77-
else:
78-
direction = "right"
79-
elif direction == "left":
80-
if col - 1 >= 0 and result[row][col - 1] == 0:
81-
direction = "left"
82-
else:
83-
direction = "up"
64+
if direction == "right" and not (col + 1 < size and result[row][col + 1] == 0):
65+
direction = "down"
66+
elif direction == "down" and not (row + 1 < size and result[row + 1][col] == 0):
67+
direction = "left"
68+
elif direction == "up" and not (row - 1 >= 0 and result[row - 1][col] == 0):
69+
direction = "right"
70+
elif direction == "left" and not (col - 1 >= 0 and result[row][col - 1] == 0):
71+
direction = "up"
8472

8573
return direction

0 commit comments

Comments
 (0)