Answer to Question #208214 in Python for Sudheer

Question #208214

Replace Elements with Zeros


Given a MXN matrix, write a program to replace all elements that do not belong to principal-diagonal & anti-diagonal with Zeros. Principal-diagonal elements are the set of elements of a matrix that lie on the line joining the top left corner to the bottom right corner. Anti-diagonal elements are the set of elements of a matrix that lie on the line joining the bottom left corner to the top right corner.


Input


The first line of input will contain two space separated integers, denoting MxN matrix. The next M lines will contain N space-separated integers.



Output


The output should be MXN matrix by replacing elements that do not belong to principal-diagonal and anti-diagonal with Zeros. Print each row as a space separated integers in a single line.


1
Expert's answer
2021-06-18T02:54:48-0400
m, n = input('m n ').split()
m, n = int(m), int(n)
matr = []
for i in range(m):
	matr.append(list(map(int, input().split())))
for i in range(m):
	for j in range(n):
		if not((i == j) or (i == n - j - 1)):
			matr[i][j] = 0
for el in matr:
	print(*el)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS