forked from tarunsinghofficial/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral_matrix.py
60 lines (51 loc) · 1.46 KB
/
spiral_matrix.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Write a Program to get transpose of a Matrix and print it in spiral Form
a = int(input("Enter The Number of Rows of Matrix:-"))
b = int(input("Enter The Number of Columns of Matrix:-"))
d = []
for i in range(a):
e = []
for j in range(b):
c = int(input(f"Enter {i+1}{j+1}th Element:-"))
e.append(c)
d.append(e)
def spiral_matrix(f):
k = 0
t = 0
m = len(f)
n = len(f[0])
while k < m and t < n:
for i in range(t, n):
print(f[k][i], end="\t\t")
k += 1
for i in range(k, m):
print(f[i][n-1], end="\t\t")
n -= 1
if k < m:
for i in range(n-1, (t-1), -1):
print(f[m-1][i], end="\t\t")
m -= 1
if t < n:
for i in range(m-1, k-1, -1):
print(f[i][t], end="\t\t")
t += 1
def print_matrix(z): # This function print the values of list in the list in form of Matrix
for i in range(len(z)):
for j in range(len(z[0])):
print(z[i][j], end="\t\t")
print()
print()
def transpose(f):
u = []
for w in range(len(f[0])):
v = []
for q in range(len(f)):
v.append(f[q][w])
u.append(v)
return u
print("Original Matrix:-")
print_matrix(d)
print("Transposed Matrix:-")
e = transpose(d)
print_matrix(e)
print("Transposed Matrix in Spiral Form:-")
spiral_matrix(e)