Answer to Question #296191 in Python for Ram

Question #296191

Area of a Sub-matrix


You are given an M*N matrix and K, write a program to compute the area of the sub-matrix and print the result.

A sub-matrix is a matrix formed after deleting K rows each from the top and bottom and K columns each from left and right.

Area of a matrix is defined as the product of all elements of the matrix.


Explanation

For M = 5 and N = 4, the matrix is:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

17 18 19 20

For K = 1, the sub-matrix is as follows:

6 7

10 11

14 15

Therefore, the area of the matrix is 6*7*10*11*14*15 = 970200.



Sample Input1

5 4

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

17 18 19 20

1


Sample Output1

970200


1
Expert's answer
2022-02-10T09:48:59-0500
def calc_area(matrix, k):
	if k > len(matrix)//2 or k > len(matrix[0])//2:
		return None
	res = 1
	for i in range(k, len(matrix)-k):
		for j in range(k, len(matrix[i])-k):
			res *= matrix[i][j]
	return res


n, m = input("n m: ").split()
n, m = int(n), int(m)
matr = [[int(i) for i in input(f"{_+1} row: ").split()] for _ in range(n)]
k = int(input("k: "))


print(calc_area(matr,k))

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