Skip to content

Commit f87bd4c

Browse files
committed
Update spiral_matrix.py
1 parent 48f6d10 commit f87bd4c

File tree

1 file changed

+5
-18
lines changed

1 file changed

+5
-18
lines changed

spiral-matrix/spiral_matrix.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,20 @@ def spiral_matrix(size: int) -> list:
3737
while num <= max_num:
3838
result[x_y[0]][x_y[1]] = num
3939
direction = _set_direction(direction, result, x_y, size)
40-
x_y = _set_x_y(x_y, direction)
40+
x_y[0] += DIRECTIONS[direction]["row"]
41+
x_y[1] += DIRECTIONS[direction]["col"]
4142
num += 1
4243

4344
return result
4445

4546

46-
def _set_x_y(x_y: list, direction: str) -> list:
47-
"""
48-
Advance the current coordinates by one step in the given direction.
49-
50-
:param x_y: Current position as ``[row, col]`` which will be mutated and
51-
returned.
52-
:param direction: One of ``{"right", "left", "up", "down"}``.
53-
:return: The updated ``[row, col]`` coordinates after moving one step.
54-
"""
55-
x_y[0] += DIRECTIONS[direction]["row"]
56-
x_y[1] += DIRECTIONS[direction]["col"]
57-
return x_y
58-
59-
6047
def _set_direction(direction: str, result: list, x_y: list, size: int) -> str:
6148
"""
6249
Determine the next movement direction based on bounds and visited cells.
6350
64-
:param direction: Current direction: one of ``{"right", "left", "up", "down"}``.
65-
:param result: The current matrix being filled; zeros denote unvisited cells.
66-
:param x_y: Current coordinates as ``[row, col]``.
51+
:param direction: next direction, one of {"right", "left", "up", "down"}.
52+
:param result: The current matrix being filled.
53+
:param x_y: Current coordinates as [row, col].
6754
:param size: Dimension of the square matrix.
6855
:return: The direction to move next so that the spiral proceeds without
6956
leaving bounds or revisiting filled cells.

0 commit comments

Comments
 (0)