Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe4150c

Browse files
brajesh-ritpoyea
authored andcommitted
Create spiralPrint.py (#844)
* Create spiralPrint.py* Update spiralPrint.py
1 parentb7cff04 commitbe4150c

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+
defcheckMatrix(a):
10+
# must be
11+
iftype(a)==listandlen(a)>0:
12+
iftype(a[0])==list:
13+
prevLen=0
14+
foriina:
15+
ifprevLen==0:
16+
prevLen=len(i)
17+
result=True
18+
elifprevLen==len(i):
19+
result=True
20+
else:
21+
result=False
22+
else:
23+
result=True
24+
else:
25+
result=False
26+
returnresult
27+
28+
29+
defspiralPrint(a):
30+
31+
ifcheckMatrix(a)andlen(a)>0:
32+
33+
matRow=len(a)
34+
iftype(a[0])==list:
35+
matCol=len(a[0])
36+
else:
37+
fordatina:
38+
print(dat),
39+
return
40+
41+
# horizotal printing increasing
42+
foriinrange(0,matCol):
43+
print(a[0][i]),
44+
# vertical printing down
45+
foriinrange(1,matRow):
46+
print(a[i][matCol-1]),
47+
# horizotal printing decreasing
48+
ifmatRow>1:
49+
foriinrange(matCol-2,-1,-1):
50+
print(a[matRow-1][i]),
51+
# vertical printing up
52+
foriinrange(matRow-2,0,-1):
53+
print(a[i][0]),
54+
remainMat= [row[1:matCol-1]forrowina[1:matRow-1]]
55+
iflen(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)

[8]ページ先頭

©2009-2025 Movatter.jp