2019-06-26 15:57:08 +00:00
|
|
|
"""
|
2020-01-18 12:24:33 +00:00
|
|
|
This program print the matrix in spiral form.
|
2019-06-26 15:57:08 +00:00
|
|
|
This problem has been solved through recursive way.
|
|
|
|
Matrix must satisfy below conditions
|
|
|
|
i) matrix should be only one or two dimensional
|
2021-08-28 18:07:10 +00:00
|
|
|
ii) number of column of all rows should be equal
|
2019-06-26 15:57:08 +00:00
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-08-28 18:07:10 +00:00
|
|
|
|
2022-10-04 18:05:56 +00:00
|
|
|
def check_matrix(matrix: list[list[int]]) -> bool:
|
2019-06-26 15:57:08 +00:00
|
|
|
# must be
|
2022-10-04 18:05:56 +00:00
|
|
|
matrix = list(list(row) for row in matrix)
|
|
|
|
if matrix and isinstance(matrix, list):
|
|
|
|
if isinstance(matrix[0], list):
|
2021-08-28 18:07:10 +00:00
|
|
|
prev_len = 0
|
|
|
|
for row in matrix:
|
|
|
|
if prev_len == 0:
|
|
|
|
prev_len = len(row)
|
2019-06-26 15:57:08 +00:00
|
|
|
result = True
|
|
|
|
else:
|
2021-08-28 18:07:10 +00:00
|
|
|
result = prev_len == len(row)
|
2019-06-26 15:57:08 +00:00
|
|
|
else:
|
|
|
|
result = True
|
|
|
|
else:
|
|
|
|
result = False
|
2021-08-28 18:07:10 +00:00
|
|
|
|
2019-06-26 15:57:08 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2022-10-04 18:05:56 +00:00
|
|
|
def spiral_print_clockwise(a: list[list[int]]) -> None:
|
|
|
|
"""
|
|
|
|
>>> 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
|
|
|
|
"""
|
2021-08-28 18:07:10 +00:00
|
|
|
if check_matrix(a) and len(a) > 0:
|
2022-10-04 18:05:56 +00:00
|
|
|
a = list(list(row) for row in a)
|
|
|
|
mat_row = len(a)
|
|
|
|
if isinstance(a[0], list):
|
|
|
|
mat_col = len(a[0])
|
2019-06-26 15:57:08 +00:00
|
|
|
else:
|
|
|
|
for dat in a:
|
2022-10-04 18:05:56 +00:00
|
|
|
print(dat)
|
2019-06-26 15:57:08 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# horizotal printing increasing
|
2022-10-04 18:05:56 +00:00
|
|
|
for i in range(0, mat_col):
|
|
|
|
print(a[0][i])
|
2019-06-26 15:57:08 +00:00
|
|
|
# vertical printing down
|
2022-10-04 18:05:56 +00:00
|
|
|
for i in range(1, mat_row):
|
|
|
|
print(a[i][mat_col - 1])
|
2019-06-26 15:57:08 +00:00
|
|
|
# horizotal printing decreasing
|
2022-10-04 18:05:56 +00:00
|
|
|
if mat_row > 1:
|
|
|
|
for i in range(mat_col - 2, -1, -1):
|
|
|
|
print(a[mat_row - 1][i])
|
2019-06-26 15:57:08 +00:00
|
|
|
# vertical printing up
|
2022-10-04 18:05:56 +00:00
|
|
|
for i in range(mat_row - 2, 0, -1):
|
|
|
|
print(a[i][0])
|
|
|
|
remain_mat = [row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
|
|
|
|
if len(remain_mat) > 0:
|
|
|
|
spiral_print_clockwise(remain_mat)
|
2019-06-26 15:57:08 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print("Not a valid matrix")
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# driver code
|
2021-08-28 18:07:10 +00:00
|
|
|
if __name__ == "__main__":
|
2022-10-04 18:05:56 +00:00
|
|
|
a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
|
|
|
|
spiral_print_clockwise(a)
|