-
Notifications
You must be signed in to change notification settings - Fork 2
/
Matrix-Questions.py
172 lines (166 loc) · 4.12 KB
/
Matrix-Questions.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Create 2D and 3D list
l=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row.append(e)
l.append(row)
print("2D List created:",l)
print("3D List=[")
for i in range(r):
print("\t [",end=" ")
for j in range(c):
print(l[i][j],end=" ")
print("]")
print("\t]")
# Create matrix of square 2D list & find sum of elements of first row and diagonals
l=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row.append(e)
l.append(row)
print("2D List created:",l)
print("3D List: [")
for i in range(r):
print("\t [",end=" ")
for j in range(c):
print(l[i][j],end=" ")
print("]")
print("\t ]")
print('Sum of elements in first row:',sum(l[0]))
s=0
for k in range(r):
s+=l[k][k]
print('Sum of elements in diagonals:',s)
# Create 2 matrices & find sum
l1=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
print('---------- LIST 1 ----------')
for i in range(r):
row1=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row1.append(e)
l1.append(row1)
print("2D List created:",l1)
print("3D List: [")
for i in range(r):
print("\t [",end="")
for j in range(c):
print(l1[i][j],end=" ")
print("]")
print("\t ]")
print('---------- LIST 2 ----------')
l2=[]
for i in range(r):
row2=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row2.append(e)
l2.append(row2)
print("2D List created:",l2)
print("3D List: [")
for i in range(r):
print("\t [",end="")
for j in range(c):
print(l2[i][j],end=" ")
print("]")
print("\t ]")
s=[[l1[i][j]+l2[i][j] for j in range(len(l1[0]))] for i in range(len(l1))]
for k in s:
print(s)
# Write a program to print the elements of upper triangle and lower triangle
l=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row.append(e)
l.append(row)
print("2D List created:",l)
print("3D List: [")
for i in range(r):
print("\t [",end="")
for j in range(c):
print(l[i][j],end=" ")
print("]")
print("\t ]")
print('Elements in lower triangle:')
for i in range(0,r):
for j in range(0,c):
if i<j:
print(' ',end=' ')
else:
print(l[i][j],end=' ')
print(' ')
print('Elements in upper triangle:')
for i in range(0,r):
for j in range(0,c):
if i>j:
print(' ',end=' ')
else:
print(l[i][j],end=' ')
print(' ')
# Transpose matrix
l=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row.append(e)
l.append(row)
print("2D List created:",l)
print("3D List: [")
for i in range(r):
print("\t [",end=" ")
for j in range(c):
print(l[i][j],end=" ")
print("]")
print("\t ]")
print('Transposed list: [')
for i in range(c):
print("\t\t [",end=" ")
for j in range(r):
print(l[j][i],end=" ")
print("]")
print("\t\t ]")
# Sum of given positions from matrix
l=[]
r=int(input("Enter number of rows: "))
c=int(input("Enter number of columns: "))
for i in range(r):
row=[]
for j in range(c):
e=int(input("Element at "+str(i)+","+str(j)+": "))
row.append(e)
l.append(row)
print("2D List created:",l)
print("3D List: [")
for i in range(r):
print("\t [",end=" ")
for j in range(c):
print(l[i][j],end=" ")
print("]")
print("\t ]")
n=int(input('Enter number of elements you want to find sum of: '))
s=0
for i in range(n):
e=input('Enter position of element: ')
rn=int(e[0])
cn=int(e[2])
s+=int(l[rn][cn])
print('Sum of elements:',s)
# Multiply matrices
# Upper triangle sum