phatboislym

Untitled

Dec 19th, 2023 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | Source Code | 0 0
  1. class Solution:
  2.     def imageSmoother(self, img: list[list[int]]) -> list[list[int]]:
  3.         def createFilter(img: list[list], i: int, j: int) -> int:
  4.             grid_sum: int = 0
  5.             count: int = 0
  6.             size: int = len(img)
  7.             breadth: int = len(img[0])
  8.            
  9.             for row in range(i-1, i+2):
  10.                 for col in range(j-1, j+2):
  11.                     if row >= 0 and row < size and col >= 0 and col < breadth:
  12.                         grid_sum += img[row][col]
  13.                         count += 1
  14.            
  15.             return int(grid_sum / count)
  16.  
  17.         rows: range = range(len(img))
  18.         cols: range = range(len(img[0]))
  19.         out: list = [ [None for col in cols] for row in rows]
  20.  
  21.         for row in rows:
  22.             for col in cols:
  23.                 out[row][col] = createFilter(img, row, col)
  24.  
  25.         return out
  26.      
  27. img: list = [ [100, 200, 100], [200, 50, 200], [100, 200, 100] ]
  28. expected: list = [ [137, 141, 137], [141, 138, 141], [137, 141, 137] ]
  29.  
  30. solution: Solution = Solution()
  31. out: list = solution.imageSmoother(img=img)
  32. print(out)
  33. print(out == expected)
Advertisement
Add Comment
Please, Sign In to add comment