forked from geekquad/AlgoBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added printing Matrix in Spiral geekquad#20
- Loading branch information
1 parent
87b3e18
commit b5d44f2
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
def Spiralprint(matrix): | ||
|
||
top = left = 0 # initializing with top | ||
bottom = len(matrix) - 1 | ||
right = len(matrix[0]) - 1 | ||
|
||
while True: | ||
if left > right: | ||
break | ||
|
||
# print top row | ||
for i in range(left, right + 1): | ||
print(matrix[top][i], end=' ') | ||
top = top + 1 | ||
|
||
if top > bottom: | ||
break | ||
|
||
# print right column | ||
for i in range(top, bottom + 1): | ||
print(matrix[i][right], end=' ') | ||
right = right - 1 | ||
|
||
if left > right: | ||
break | ||
|
||
# print bottom row | ||
for i in range(right, left - 1, -1): | ||
print(matrix[bottom][i], end=' ') | ||
bottom = bottom - 1 | ||
|
||
if top > bottom: | ||
break | ||
|
||
# print left column | ||
for i in range(bottom, top - 1, -1): | ||
print(matrix[i][left], end=' ') | ||
left = left + 1 | ||
|
||
|
||
rows = int(input()) | ||
cols = int(input()) | ||
matrix = [] | ||
for i in range(0, rows): | ||
arr = list(map(int, input().split()[:cols])) | ||
matrix.append(arr) | ||
|
||
Spiralprint(matrix) |