MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / spiral_print_clockwise

Function spiral_print_clockwise

matrix/spiral_print.py:30–76  ·  view source on GitHub ↗

>>> spiral_print_clockwise([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) 1 2 3 4 8 12 11 10 9 5 6 7

(a: list[list[int]])

Source from the content-addressed store, hash-verified

28
29
30def spiral_print_clockwise(a: list[list[int]]) -> None:
31 """
32 >>> spiral_print_clockwise([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
33 1
34 2
35 3
36 4
37 8
38 12
39 11
40 10
41 9
42 5
43 6
44 7
45 """
46 if check_matrix(a) and len(a) > 0:
47 a = [list(row) for row in a]
48 mat_row = len(a)
49 if isinstance(a[0], list):
50 mat_col = len(a[0])
51 else:
52 for dat in a:
53 print(dat)
54 return
55
56 # horizotal printing increasing
57 for i in range(mat_col):
58 print(a[0][i])
59 # vertical printing down
60 for i in range(1, mat_row):
61 print(a[i][mat_col - 1])
62 # horizotal printing decreasing
63 if mat_row > 1:
64 for i in range(mat_col - 2, -1, -1):
65 print(a[mat_row - 1][i])
66 # vertical printing up
67 for i in range(mat_row - 2, 0, -1):
68 print(a[i][0])
69 remain_mat = [row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
70 if len(remain_mat) > 0:
71 spiral_print_clockwise(remain_mat)
72 else:
73 return
74 else:
75 print("Not a valid matrix")
76 return
77
78
79# Other Easy to understand Approach

Callers 1

spiral_print.pyFile · 0.85

Calls 1

check_matrixFunction · 0.85

Tested by

no test coverage detected