Advertisement
simeonshopov

maximum sum

May 29th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. #!/usr/local/bin/python3.7
  2. # -*- coding: utf-8 -*import
  3.  
  4. rows, columns = map(int, input().split())
  5.  
  6. matrix = [[int(x) for x in input().split()] for _ in range(rows)]
  7.  
  8. row = 0
  9. col = 0
  10. rows_count = rows - 3
  11. columns_count = columns - 3
  12.  
  13.  
  14. def find_squares(r: int, c: int, r_c: int, c_c: int):
  15.     s = []
  16.     while True:
  17.         if r == r_c and c > c_c:
  18.             break
  19.         if c > c_c:
  20.             r += 1
  21.             c = 0
  22.         j = 0
  23.         k = 0
  24.         sq = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  25.         for i in range(r, r + 3):
  26.             for y in range(c, c + 3):
  27.                 sq[j][k] = matrix[i][y]
  28.                 k += 1
  29.             j += 1
  30.             k = 0
  31.         s.append(sq)
  32.         c += 1
  33.     return s
  34.  
  35.  
  36. squares = find_squares(row, col, rows_count, columns_count)
  37.  
  38. sums = [sum(sum(x) for x in y) for y in squares]
  39. max_ones = [x for x in sums if x == max(sums)]
  40. winner = squares[sums.index(max_ones[0])]
  41.  
  42. print(f'Sum = {max_ones[0]}')
  43. print('\n'.join([' '.join([str(y) for y in x]) for x in winner]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement