Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def imageSmoother(self, img: list[list[int]]) -> list[list[int]]:
- def createFilter(img: list[list], i: int, j: int) -> int:
- grid_sum: int = 0
- count: int = 0
- size: int = len(img)
- breadth: int = len(img[0])
- for row in range(i-1, i+2):
- for col in range(j-1, j+2):
- if row >= 0 and row < size and col >= 0 and col < breadth:
- grid_sum += img[row][col]
- count += 1
- return int(grid_sum / count)
- rows: range = range(len(img))
- cols: range = range(len(img[0]))
- out: list = [ [None for col in cols] for row in rows]
- for row in rows:
- for col in cols:
- out[row][col] = createFilter(img, row, col)
- return out
- img: list = [ [100, 200, 100], [200, 50, 200], [100, 200, 100] ]
- expected: list = [ [137, 141, 137], [141, 138, 141], [137, 141, 137] ]
- solution: Solution = Solution()
- out: list = solution.imageSmoother(img=img)
- print(out)
- print(out == expected)
Advertisement
Add Comment
Please, Sign In to add comment