CodeSignal 22. BoxBlur

hjseo-dev·2021년 7월 9일
0

Python 문제풀이

목록 보기
7/7

22. BoxBlur

The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.

Return the blurred image as an integer, with the fractions rounded down.

Example

For

image = [[1, 1, 1], 
         [1, 7, 1], 
         [1, 1, 1]]
the output should be boxBlur(image) = [[1]].

To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.

주어진 행렬에서 3X3마다 총합을 구하고 9로 나눈 수를 다시 행렬로 바꿔서 출력하기

💡 문제풀이
1. 주어진 행렬 안에서 3*3행렬을 구하기 위해 행과 열의 개수에서 -2씩 하여 찾는다
2. 각 행렬에서 3번째 까지 루프를 돌며 합을 계산해 리스트에 넣는다
3. 각 리스트를 행렬로 표현한다

def boxBlur(image):
    return [[sum(sum(x[i:i+3]) for x in image[j:j+3])/9 
                                   for i in range(len(image[0])-2)] 
                                   for j in range(len(image)-2)]

최적화 된 풀이는 한번에 리턴!

0개의 댓글