Answer to Question #196714 in Python for Santhoshreddy

Question #196714

Max continues matrix


1
Expert's answer
2021-05-21T11:52:39-0400
def matr_sum(mat:list):
	s = 0
	for row in mat:
		for el in row:
			s += el
	return s


def sub_size(length:int):


	for start in range(length):
		for end in range(start+1, length+1):
			yield (start, end)


def max_matr_sum(matr:list, m:int, n:int):


	max_s = 0
	max_m = matr
	for start_x, end_x in sub_size(m):
		for start_y, end_y in sub_size(n):
			tmp_matr = [i[start_y:end_y] for i in matr[start_x:end_x]]
			if matr_sum(tmp_matr) > max_s:
				max_s = matr_sum(tmp_matr)
				max_m = tmp_matr
	print(max_s)
	for el in max_m:
		print(*el)




while True:


	m, n = (map(int, input('m n ').split()))
	matrix = list([] for i in range(n))
	i = 0
	while i < m:
		try:
			tmp = list(map(int, input().split()))
			if len(tmp) != n:
				raise ValueError
			matrix[i] = tmp
			i += 1
		except ValueError:
			print('incorrecr input')
			continue


	max_matr_sum(matrix, m, n)

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