forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralprint.py
More file actions
45 lines (45 loc) · 1.65 KB
/
spiralprint.py
File metadata and controls
45 lines (45 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Spiral Print
# ------------------------
# Given an N*M 2D array, print it in spiral form. That is, first you need to print the 1st row, then last column, then last row and then first column and so on.
# Print every element only once.
# Input format :
# Line 1 : N and M, No. of rows & No. of columns (separated by space) followed by N*M elements in row wise fashion.
# Output format :
# Elements of matrix in spiral form in a single line and space separated
# Constraints :
# 0 <= N <= 10^4
# 0 <= M <= 10^4
# Sample Input 1:
# 4 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Sample Output 1:
# 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
def spiralPrint(arr):
rows = len(arr)
cols = len(arr[0])
startRow, startCol, endRow, endCol = 0, 0, rows-1, cols-1
while startRow<=endRow and startCol<=endCol:
# Print startRow
for j in range(startCol, endCol+1):
print(arr[startRow][j], end=' ')
startRow += 1
if startRow>endRow or startCol>endCol:
break
# Print endCol
for i in range(startRow, endRow+1):
print(arr[i][endCol], end=' ')
endCol -= 1
if startRow>endRow or startCol>endCol:
break
for j in range(endCol, startCol-1, -1):
print(arr[endRow][j], end=' ')
endRow -= 1
if startRow>endRow or startCol>endCol:
break
# Print startCol
for i in range(endRow, startRow-1, -1):
print(arr[i][startCol], end=' ')
startCol += 1
l=[int(i) for i in input().strip().split(' ')]
m, n=l[0], l[1]
arr = [ [ l[(j*n)+i+2] for i in range(n)] for j in range(m)]
spiralPrint(arr)