본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 93

반응형

Q>

On the chest keypad is a grid of numbered dots.

(가슴 키패드에는 번호가 매겨진 점들로 구성된 격자가 있습니다.)

The grid is comprised of a square shaped array of dots and contains lines that connect some pairs of adjacent dots.

(그리드는 정사각형 모양의 도트 배열로 구성되며 인접한 도트 쌍을 ​​연결하는 선을 포함합니다.)

The answer to the code is the number of squares that are formed by these lines.

(코드에 대한 대답은이 선들에 의해 형성된 제곱의 수입니다.)

For example, in the figure shown below, there are 3 squares: 2 small squares and 1 medium square.

(예를 들어, 아래 그림에는 3 개의 정사각형이 있습니다. 2 개의 작은 정사각형과 1 개의 중간 정사각형입니다.)

The dots are marked by the numbers 1 through 16.

(점들에는 1에서 16까지의 숫자가 표시됩니다.)

The endpoints of the lines are represented by lists of two numbers.

(선의 끝점은 두 개의 숫자 목록으로 표시됩니다.)

 

Input: A list of lines as a list of list. Each list consists of the two integers.

        (리스트의리스트로서의 라인리스트. 각 목록은 두 개의 정수로 구성됩니다.)

 

Output: The quantity of squares formed as an integer.

           (정수로 형성된 제곱의 양.)

 

Example:

checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
     [7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
     [10, 14], [12, 16], [14, 15], [15, 16]]) == 3
checkio([[1, 2], [2, 3], [3, 4], [1, 5], [4, 8],
     [6, 7], [5, 9], [6, 10], [7, 11], [8, 12],
     [9, 13], [10, 11], [12, 16], [13, 14], [14, 15], [15, 16]]) == 2

 

How it is used: This is a simple puzzle that illustrates pattern searching.

                     (이것은 패턴 검색을 보여주는 간단한 퍼즐입니다.)

                     For complex cases you can improve your program and use it to search for more complex patterns,

                     (복잡한 경우에는 프로그램을 개선하고 더 복잡한 패턴을 검색하는 데 사용할 수 있습니다.)

                     shapes and objects.

                     (모양 및 개체.)

 

Precondition: 0 < len(lines) ≤ 32

 

A>

from typing import List

def is_square(w, n, list):
    if n == 1:
        if [w,w+n] in list and [w+4, w+4+n] in list and [w, w+4] in list and [w+n, w+n+4] in list:
            return 1
        else:
            return 0
    if n==2:
        if [w,w+1] in list and [w+1, w+2] in list and [w+8, w+9] in list and [w+9, w+10] in list and [w,w+4] in list and [w+4, w+8] in list and [w+2, w+6] in list and [w+6, w+10] in list:
            return 1
        else:
            return 0
    if n==3:
        if [1,2] in list and [2,3] in list and [3,4] in list and [4,8] in list and [8,12] in list and [12,16] in list and [1,5] in list and [5,9] in list and [9,13] in list and [13, 14] in list and [14, 15] in list and [15,16] in list:
            return 1
        else:
            return 0
            
def checkio(lines_list):
    res = 0
    tmp = lines_list
    lines_list = []
    for el in tmp:
        lines_list.append(sorted(el))
        
    for i in range(1,4):
        if is_square(i, 1, lines_list):
            res +=1
        if is_square(i+4, 1, lines_list):
            res +=1
        if is_square(i+8, 1, lines_list):
            res +=1
    for i in range(1,3):
        if is_square(i, 2, lines_list):
            res +=1
        if is_square(i+4, 2, lines_list):
            res +=1
    if is_square(i, 3, lines_list):
        res +=1
    
    return res

if __name__ == '__main__':
    print("Example:")
    print(checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
                   [7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
                   [10, 14], [12, 16], [14, 15], [15, 16]]))

    assert (checkio([[1, 2], [3, 4], [1, 5], [2, 6], [4, 8], [5, 6], [6, 7],
                     [7, 8], [6, 10], [7, 11], [8, 12], [10, 11],
                     [10, 14], [12, 16], [14, 15], [15, 16]]) == 3), "First, from description"
    assert (checkio([[1, 2], [2, 3], [3, 4], [1, 5], [4, 8],
                     [6, 7], [5, 9], [6, 10], [7, 11], [8, 12],
                     [9, 13], [10, 11], [12, 16], [13, 14], [14, 15], [15, 16]]) == 2), "Second, from description"
    assert (checkio([[1, 2], [1, 5], [2, 6], [5, 6]]) == 1), "Third, one small square"
    assert (checkio([[1, 2], [1, 5], [2, 6], [5, 9], [6, 10], [9, 10]]) == 0), "Fourth, it's not square"
    assert (checkio([[16, 15], [16, 12], [15, 11], [11, 10],
                     [10, 14], [14, 13], [13, 9]]) == 0), "Fifth, snake"
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

Example:
3
Coding complete? Click 'Check' to earn cool rewards!

 

S>

https://py.checkio.org

반응형

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

Python Learn the basics Quiz 95  (0) 2019.06.24
Python Learn the basics Quiz 94  (0) 2019.06.24
Python Learn the basics Quiz 92  (0) 2019.06.22
Python Learn the basics Quiz 91  (0) 2019.06.21
Python Learn the basics Quiz 90  (0) 2019.06.21