Skip to content

Commit be4150c

Browse files
brajesh-ritpoyea
authored andcommitted
Create spiralPrint.py (#844)
* Create spiralPrint.py * Update spiralPrint.py
1 parent b7cff04 commit be4150c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

matrix/spiralPrint.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
This program print the matix in spiral form.
3+
This problem has been solved through recursive way.
4+
5+
Matrix must satisfy below conditions
6+
i) matrix should be only one or two dimensional
7+
ii)column of all the row should be equal
8+
"""
9+
def checkMatrix(a):
10+
# must be
11+
if type(a) == list and len(a) > 0:
12+
if type(a[0]) == list:
13+
prevLen = 0
14+
for i in a:
15+
if prevLen == 0:
16+
prevLen = len(i)
17+
result = True
18+
elif prevLen == len(i):
19+
result = True
20+
else:
21+
result = False
22+
else:
23+
result = True
24+
else:
25+
result = False
26+
return result
27+
28+
29+
def spiralPrint(a):
30+
31+
if checkMatrix(a) and len(a) > 0:
32+
33+
matRow = len(a)
34+
if type(a[0]) == list:
35+
matCol = len(a[0])
36+
else:
37+
for dat in a:
38+
print(dat),
39+
return
40+
41+
# horizotal printing increasing
42+
for i in range(0, matCol):
43+
print(a[0][i]),
44+
# vertical printing down
45+
for i in range(1, matRow):
46+
print(a[i][matCol - 1]),
47+
# horizotal printing decreasing
48+
if matRow > 1:
49+
for i in range(matCol - 2, -1, -1):
50+
print(a[matRow - 1][i]),
51+
# vertical printing up
52+
for i in range(matRow - 2, 0, -1):
53+
print(a[i][0]),
54+
remainMat = [row[1:matCol - 1] for row in a[1:matRow - 1]]
55+
if len(remainMat) > 0:
56+
spiralPrint(remainMat)
57+
else:
58+
return
59+
else:
60+
print("Not a valid matrix")
61+
return
62+
63+
64+
# driver code
65+
a = [[1 , 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]
66+
spiralPrint(a)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy