Advertisement
Nenogzar

07. Square with Maximum Sum

May 22nd, 2024
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. rows, colons = list(map(int, input().split(", ")))
  2.  
  3. matrix_list = [list(map(int, input().split(", "))) for _ in range(rows)]
  4. max_sum = {"max number": float('-inf'), "row": 0, "col": 0}
  5.  
  6. def check_valid_index(row, col):
  7.     return row + 1 < rows and col + 1 < colons
  8.  
  9. def sum_square(row, col):
  10.     if check_valid_index(row, col):
  11.         sum_total = matrix_list[row][col] + matrix_list[row][col + 1] + matrix_list[row + 1][col] + matrix_list[row + 1][col + 1]
  12.         if max_sum["max number"] < sum_total:
  13.             max_sum["max number"] = sum_total
  14.             max_sum["row"] = row
  15.             max_sum["col"] = col
  16.  
  17. for row in range(rows):
  18.     for col in range(colons):
  19.         sum_square(row, col)
  20.  
  21. for row in range(max_sum["row"], max_sum["row"] + 2):
  22.     print(" ".join(map(str, matrix_list[row][max_sum["col"]: max_sum["col"] + 2])))
  23. print(max_sum['max number'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement