[Python] CodeSignal 문제 풀이 (58~60)

2020. 4. 27. 21:34Python/CodeSignal Algorithm

 

 

 

58. You are taking part in an Escape Room challenge designed specifically for programmers. In your efforts to find a clue, you've found a binary code written on the wall behind a vase, and realized that it must be an encrypted message. After some thought, your first guess is that each consecutive 8 bits of the code stand for the character with the corresponding extended ASCII code.

Assuming that your hunch is correct, decode the message.

 

[Example]

For code = "010010000110010101101100011011000110111100100001", the output should be
messageFromBinaryCode(code) = "Hello!".

The first 8 characters of the code are 01001000, which is 72 in the binary numeral system. 72 stands for H in the ASCII-table, so the first letter is H.
Other letters can be obtained in the same manner.

 

[Solution]

# <My Code>
def messageFromBinaryCode(code):
    l = []
    s = ""

    for i in range(len(code)):
        s += code[i]
        if len(s) == 8:
            l.append("0b" + s)
            s = ""
    print(l)
    print(s)
    for i in range(len(l)):
        s += chr(int(l[i], 2))
    return s

# <Best Code>
def messageFromBinaryCode(code):
    return "".join([chr(int(code[8*i:8*i+8],2)) for i in range(len(code)//8)])

 

 

59. Construct a square matrix with a size N × N containing integers from 1 to N * N in a spiral order, starting from top-left and in clockwise direction.

 

[Example]

For n = 3, the output should be

 

[Solution]

# <My Code>
def spiralNumbers(n):
    l = [[0 for col in range(n)] for row in range(n)]
    row, col = 0, -1
    flag = 1
    value = 1

    while n > 0:
        for i in range(n):
            col += flag
            l[row][col] = value
            value += 1
        n -= 1
        if n == 0: break
        for i in range(n):
            row += flag
            l[row][col] = value
            value += 1
        flag *= -1
    return l

# <Best Code>
def spiralNumbers(n):
    m = [[0] * n for i in range(n)]
    dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]
    x, y, c = 0, -1, 1
    for i in range(n + n - 1):
        for j in range((n + n - i) // 2):
            x += dx[i % 4]
            y += dy[i % 4]
            m[x][y] = c
            c += 1
    return m

 

 

60. Sudoku is a number-placement puzzle. The objective is to fill a 9 × 9 grid with digits so that each column, each row, and each of the nine 3 × 3 sub-grids that compose the grid contains all of the digits from 1 to 9.

This algorithm should check if the given grid of numbers represents a correct solution to Sudoku.

 

[Example]

 

[Solution]

# <My Code>
def sudoku(grid):
    comp = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    for i in range(9):
        l = []
        for j in range(9):
            l.append(grid[j][i])

        #cols/rows
        if comp == sorted(l) and comp == sorted(grid[i]): continue
        else:
            return False

    #sub-grids
    row, col = 0, 0
    for i in range(3):
        for j in range(3):
            l = []
            for y in range(row, row + 3):
                for x in range(col, col + 3):
                    l.append(grid[y][x])
            print(l)
            col += 3
            if comp == sorted(l): continue
            else: return False      
        col = 0
        row += 3
    return True

# <Best Code>
def sudoku(grid):

    def r(i):
        return sorted(grid[i]) != list(range(1,10))
    
    def c(i):
        return sorted([grid[x][i] for x in range(9)]) != list(range(1,10))
    
    def g(x,y):
        return sorted([grid[i][j] for i in range(x,x+3) for j in range(y,y+3)]) != list(range(1,10))

    for i in range(9):
        if r(i) or c(i):
            return False
    for i in range(0,9,3):
        for j in range(0,9,3):
            if g(i,j):
                return False
    return True