-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex.week3.py
48 lines (41 loc) · 1.01 KB
/
ex.week3.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
# how to count number of zeros in a matrix
matrix = [[0,5,1],
[6,8,0],
[4,0,7]]
count =0
for i in range (len(matrix)):
for j in range (len(matrix)):
if matrix[i][j] == 0:
count += 1
print(count)
print("#############################################")
# how to sum two 2d matrix and append the result in new matrix
m1 =[[0,5,1],
[6,8,0],
[4,0,7]]
m2 =[[5,5,6],
[2,3,4],
[7,8,2]]
def sum_matrix(m1,m2):
result = []
for i in range (len(m1)):
raws =[]
for j in range (len(m1)):
raws.append(m1[i][j]+m2[i][j])
result.append(raws)
print(result)
sum_matrix(m1,m2)
print("#############################################")
# how to print:
# 1 1 1 1
# 0 1 1 1
# 0 0 1 1
# 0 0 0 1
size = int (input("Enter the size: "))
for i in range (size):
for j in range (size):
if j>= i:
print (1, end = " ")
else:
print (0, end = " ")
print()