Advertisement
Nenogzar

matrix

May 22nd, 2024
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. def max_neighbor_sum(matrix):
  2.     max_sum = float('-inf')
  3.     rows = len(matrix)
  4.     cols = max(len(row) for row in matrix)
  5.  
  6.     for i in range(rows - 1):
  7.         for j in range(cols - 1):
  8.  
  9.             if i + 1 < rows and j + 1 < len(matrix[i]) and j + 1 < len(matrix[i + 1]):
  10.  
  11.                 current_sum = (
  12.                     matrix[i][j] + matrix[i + 1][j] +
  13.                     matrix[i][j + 1] + matrix[i + 1][j + 1]
  14.                 )
  15.  
  16.                 if current_sum > max_sum:
  17.                     max_sum = current_sum
  18.  
  19.     return max_sum
  20.  
  21.  
  22. input_matrix = [
  23.     [1, 2, 200],
  24.     [4],
  25.     [5, 6],
  26.     [7, 8, 9, 10],
  27.     [1, 5],
  28.     [12, 2, 3, 5, 5]
  29. ]
  30.  
  31. result = max_neighbor_sum(input_matrix)
  32. print("Най-голямата сума на съседни точки в матрицата е:", result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement