[Python] CodeSignal 문제 풀이 (22~24)

2020. 3. 12. 21:04Python/CodeSignal Algorithm

22. You are given an array of integers representing coordinates of obstacles situated on a straight line.

Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.

Find the minimal length of the jump enough to avoid all the obstacles.

 

[Example]

 

[Solution] - ing

#<My Code>
def avoidObstacles(inputArray):
    a = sorted(inputArray)
    Flag = False
    result = 0
    
       for i in range(min(a) + 1, max(a) + 2):
        for j in range(len(a)):
            print('i: ', i, '/ i * j: ', i * j, '/ a[i]: ', a[j])
            if(i * j == a[j]):
                Flag = False
                break
            else:
                Flag = True
                result = i
            print(Flag, result)
        if(Flag == True):
            print(result)
            return result

#<Best Code>

 

23. Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.

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]

[Solution]

#<My Code>
def boxBlur(image):
    a = image
    width = len(a[0])
    height = len(a)
    box = []

    for i in range(height - 2):
        low = []
        for j in range(width - 2):
            sumNum = 0
            for k in range(i, i + 3):
                for l in range(j, j + 3):
                    sumNum += a[k][l]
            low.append(int(sumNum / 9))
        box.append(low)
    return box

#<Best Code>
def boxBlur(m):
    r = len(m)
    c = len(m[0])
    ans = []
    for i in range(1,r-1):
        row=[]
        for j in range(1,c-1):
            row.append(sum([m[i+k][j+l] for k in [-1,0,1] for l in [-1,0,1]])//9)
        ans.append(row)
    return ans

 

24. In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

 

[Example]

 

[Solution]

#<My Code>
def minesweeper(matrix):
    mine = []
    low = len(matrix)
    col = len(matrix[0])
    
    
    for i in range(low):
        mine.append([])
        for j in range(col):
            cnt = 0
            for x in range(i - 1, i + 2):
                for y in range(j - 1, j + 2):
                    if(x < 0 or y < 0 or  low - 1 < x or col - 1 < y): continue
                    if(i == x and j == y): continue
                    if(matrix[x][y] == True): cnt += 1
            mine[i].append(cnt)
    return mine

#<Best Code>
def minesweeper(matrix):

    r = []
    
    for i in range(len(matrix)):
        r.append([])
        for j in range(len(matrix[0])):
            l = -matrix[i][j]
            for x in [-1,0,1]:
                for y in [-1,0,1]:
                    if 0<=i+x<len(matrix) and 0<=j+y<len(matrix[0]):
                        l += matrix[i+x][j+y]

            r[i].append(l)
    return r