Answer to Question #239209 in Python for sai

Question #239209

Given an MxN integer matrix, write a program to - Find all zeros and replace them with the sum of their neighbor


elements.


- After replacing the zeros, set all other elements in the corresp row and column with zeros (excluding the elements which were previously zeros).


Note: Consider the upper, lower, right and left elements as


neighboring elements.


1
Expert's answer
2021-09-19T10:27:36-0400
M, N = input('Enter the size of matrix M N: ').split()
M, N = int(M), int(N)
matrix = []
for x in range(M):
    matrix.append(list(map(int, input("Input the elements of the matrix: ").split())))
for x in range(M):
    for y in range(N):
        if not((x == y) or (x == N - y - 1)):
            matrix[x][y] = 0
for element in matrix:
    print(*element)

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