Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions data_structures/arrays/spiral_merge_array_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from typing import List

Check failure on line 1 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/spiral_merge_array_traversal.py:1:1: UP035 `typing.List` is deprecated, use `list` instead


def spiral_traverse(matrix: List[List[int]]) -> List[int]:

Check failure on line 4 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:4:49: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:4:34: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:4:29: UP006 Use `list` instead of `List` for type annotation
"""
Takes a 2D array (matrix) and returns its elements in spiral order (clockwise from the top-left).

Check failure on line 6 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/spiral_merge_array_traversal.py:6:89: E501 Line too long (101 > 88)

Uses four pointers: top, bottom, left, right to track the current layer of the matrix.

Check failure on line 8 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/spiral_merge_array_traversal.py:8:89: E501 Line too long (90 > 88)
Loops through top row → right column → bottom row → left column until all elements are visited.

Check failure on line 9 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/spiral_merge_array_traversal.py:9:89: E501 Line too long (99 > 88)
"""
result = []
if not matrix:
return result

top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1

while top <= bottom and left <= right:
# Traverse top row from left to right
for j in range(left, right + 1):
result.append(matrix[top][j])
top += 1 # move top boundary down

# Traverse right column from top to bottom
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1 # move right boundary left

if top <= bottom:
# Traverse bottom row from right to left
for j in range(right, left - 1, -1):
result.append(matrix[bottom][j])
bottom -= 1 # move bottom boundary up

if left <= right:
# Traverse left column from bottom to top
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1 # move left boundary right

return result


def spiral_merge_matrices(matrices: List[List[List[int]]]) -> List[int]:

Check failure on line 44 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:44:47: UP006 Use `list` instead of `List` for type annotation

Check failure on line 44 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:44:42: UP006 Use `list` instead of `List` for type annotation

Check failure on line 44 in data_structures/arrays/spiral_merge_array_traversal.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/spiral_merge_array_traversal.py:44:37: UP006 Use `list` instead of `List` for type annotation
"""
Accepts a list of matrices.

First, converts each matrix into its spiral order list using spiral_traverse.
Then merges them alternately: take the first element from each spiral, then the second, etc., until all are merged.
"""
merged = []
# Convert each matrix to spiral order
spirals = [spiral_traverse(m) for m in matrices]
max_len = max(len(s) for s in spirals)

# Merge elements alternately from each spiral
for i in range(max_len):
for s in spirals:
if i < len(s):
merged.append(s[i])
return merged


if __name__ == "__main__":
"""
Ensures that the example usage code runs only if the script is executed directly,
not when imported as a module.

Example Output for the given mat1 and mat2:
Merged Spiral Array: [1, 10, 2, 11, 3, 12, 6, 15, 9, 18, 8, 17, 7, 16, 4, 13, 5, 14]
"""
mat1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

mat2 = [[10, 11, 12], [13, 14, 15], [16, 17, 18]]

merged = spiral_merge_matrices([mat1, mat2])
print("Merged Spiral Array:", merged)
Loading