본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 107

반응형

Q>

“There’s nothing here...” sighed Nikola.

("여기 엔 아무것도 없어."니콜라가 한숨을 쉬었다.)

“You’re kidding right? All treasure is buried treasure! It wouldn’t be treasure otherwise!” Said

("너 농담하는거야? 모든 보물은 보물에 묻혀있다! 그렇지 않으면 보물이되지 않을 것입니다! ")

Sofia. “Here, take these.” She produced three shovels from a backpack that seemed to appear out of thin air.

(소피아. "여기, 가져 가세요."그녀는 배낭에서 세 개의 삽을 만들었다.)

“Where did you get-”

("어디서 났니?" )

“Don’t ask questions. Just dig!” She hopped on the shovel and began digging furiously.

("질문하지 마라. 그냥 발굴! "그녀는 삽으로 건너 뛰고 격렬하게 파기 시작했다.)

CLUNK

“Hey we hit something.” Stephen exclaimed in surprise.

("이봐, 우리가 뭔가를 쳤어."스티븐은 놀랍게 외쳤다.)

“It’s the treasure!” Sofia was jumping up and down in excitement.

("그것은 보물입니다!"소피아는 흥분으로 위아래로 뛰어 오르고있었습니다.)

The trio dug around the treasure chest and pulled it out of the hole and wiped the dirt off.

(트리오는 보물 상자 주위를 파고 그것을 구멍에서 꺼내 먼지를 닦았다.)

Sofia tried grabbing the lid but it was locked. Nikola studied the locking mechanism.

(소피아는 뚜껑을 잡으려고했지만 잠겼습니다. Nikola는 잠금 메커니즘을 연구했습니다.)

“I’ve seen this type of lock before. It’s pretty simple. We just need to check whether there is a sequence of 4 or more matching numbers and output a bool.”

("이전에 이런 종류의 잠금 장치를 보았습니다. 꽤 간단합니다. 일치하는 숫자가 4 개 이상 있는지 확인하고 bool을 출력하면됩니다. ")

“Easy enough. Let’s open this sucker up!”

("충분히 쉬워. 이 빨판을 열어 봐! ")

Sofia was shaking in excitement.

(소피아는 흥분 속에 떨고 있었다.)

You are given a matrix of NxN (4≤N≤10).

(NxN (4≤N≤10) 행렬을받습니다.)

You should check if there is a sequence of 4 or more matching digits.

(일치하는 숫자가 4 개 이상인 순서가 있는지 확인해야합니다.)

The sequence may be positioned horizontally, vertically or diagonally (NW-SE or NE-SW diagonals).

(시퀀스는 수평, 수직 또는 대각선으로 배치 될 수 있습니다 (NW-SE 또는 NE-SW 대각선).)

 

Input: A matrix as a list of lists with integers.

         (정수가있는리스트의리스트로서의 매트릭스.)

 

Output: Whether or not a sequence exists as a boolean.

           (시퀀스가 ​​boolean.tjguscjf로 존재하는지 여부)

 

Example:

checkio([
    [1, 2, 1, 1],
    [1, 1, 4, 1],
    [1, 3, 1, 6],
    [1, 7, 2, 5]
]) == True
checkio([
    [7, 1, 4, 1],
    [1, 2, 5, 2],
    [3, 4, 1, 3],
    [1, 1, 8, 1]
]) == False
checkio([
    [2, 1, 1, 6, 1],
    [1, 3, 2, 1, 1],
    [4, 1, 1, 3, 1],
    [5, 5, 5, 5, 5],
    [1, 1, 3, 1, 1]
]) == True
checkio([
    [7, 1, 1, 8, 1, 1],
    [1, 1, 7, 3, 1, 5],
    [2, 3, 1, 2, 5, 1],
    [1, 1, 1, 5, 1, 4],
    [4, 6, 5, 1, 3, 1],
    [1, 1, 9, 1, 2, 1]
    ]) == True

 

How it is used: This concept is useful for games where you need to detect various lines of the same elements

                     (이 개념은 동일한 요소의 다양한 라인을 감지해야하는 게임에 유용합니다.)

                     (match 3 games for example).

                     ((예를 들어 3 게임 일치).)

                     This algorithm can be used for basic pattern recognition.

                     (이 알고리즘은 기본 패턴 인식에 사용할 수 있습니다.)

 

Precondition:
0 ≤ len(matrix) ≤ 10
all(all(0 < x < 10 for x in row) for row in matrix)

 

A>

def checkio(matrix):
    n = len(matrix)

    def seq_len(x, y, dx, dy, num):
        if 0 <= x < n and 0 <= y < n and matrix[y][x] == num:
            return 1 + seq_len(x + dx, y + dy, dx, dy, num)
        else:
            return 0

    dir = [(dx, dy) for dy in range(-1, 2)
           for dx in range(-1, 2)
           if dx != 0 or dy != 0]

    # 갯수가 >4 경우 True 반환
    for y in range(n):
        for x in range(n):
            for dx, dy in dir:
                if seq_len(x, y, dx, dy, matrix[y][x]) >= 4:
                    return True

    return False

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio([
        [1, 2, 1, 1],
        [1, 1, 4, 1],
        [1, 3, 1, 6],
        [1, 7, 2, 5]
    ]) == True, "Vertical"
    assert checkio([
        [7, 1, 4, 1],
        [1, 2, 5, 2],
        [3, 4, 1, 3],
        [1, 1, 8, 1]
    ]) == False, "Nothing here"
    assert checkio([
        [2, 1, 1, 6, 1],
        [1, 3, 2, 1, 1],
        [4, 1, 1, 3, 1],
        [5, 5, 5, 5, 5],
        [1, 1, 3, 1, 1]
    ]) == True, "Long Horizontal"
    assert checkio([
        [7, 1, 1, 8, 1, 1],
        [1, 1, 7, 3, 1, 5],
        [2, 3, 1, 2, 5, 1],
        [1, 1, 1, 5, 1, 4],
        [4, 6, 5, 1, 3, 1],
        [1, 1, 9, 1, 2, 1]
    ]) == True, "Diagonal"

 

O>

Pass: checkio([

[1,1,7,2,8,1,9,2,6,2],

[3,7,6,6,7,4,7,5,3,7],

[2,4,8,5,3,9,4,9,4,4],

[3,1,4,9,8,9,5,3,6,8],

[4,5,6,4,7,3,9,1,9,9],

[7,7,6,4,8,9,2,3,5,7],

[9,1,2,1,1,7,9,2,6,4],

[5,7,6,9,5,3,5,4,4,9],

[5,4,7,8,9,3,1,4,5,1],

[9,5,7,9,4,7,4,5,8,8]])

 

 Your result: true

 

S>

https://py.checkio.org

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 109  (0) 2019.06.28
Python Learn the basics Quiz 108  (0) 2019.06.27
Python Learn the basics Quiz 106  (0) 2019.06.27
Python Learn the basics Quiz 105  (0) 2019.06.25
Python Learn the basics Quiz 104  (0) 2019.06.25