본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 113

반응형

Q>

Sometimes humans build weird things.

(때로는 인간이 이상한 것을 만들어내는 경우가 있습니다.)

Our Robots have discovered and wish to use an ancient circular cannon loading system.

(우리의 로봇은 고대의 원형 캐논 로딩 시스템을 발견하고 사용하기를 원했습니다.)

This system looks like numbered pipes arranged in a circular manner.

(이 시스템은 순환 방식으로 배열 된 번호가 매겨진 파이프처럼 보입니다.)

There is a rotating mechanism behind these pipes, and the cannons are attached to the end.

(이 파이프 뒤에는 회전 메커니즘이 있으며, 대포는 끝에 붙어 있습니다.)

This system is incredibly ancient and some of the cannons are broken.

(이 시스템은 엄청나게 고대이고 대포 중 일부는 고장났습니다.)

The loading automaton has a program with the pipe numbers which indicate where it should place cannonballs.

(적재하는 자동 장치에는 대포알을 넣어야하는 곳을 나타내는 파이프 번호가있는 프로그램이 있습니다.)

These numbers cannot be changed as they are engraved into the pipes.

(이 번호는 파이프에 새겨 져 있으므로 변경할 수 없습니다.)

We can, however, rotate the backend mechanism to change the correspondence between pipes and cannons.

(그러나 파이프와 대포 간의 통신을 변경하기 위해 백엔드 메커니즘을 순환시킬 수 있습니다.)

We should find each combination that we can rotate the backend mechanism so that all loaded cannonballs will be loaded into the still-working cannons.

(우리는 백엔드 메커니즘을 회전시킬 수있는 각 조합을 찾아 모든 적재 된 캐논볼이 여전히 작동하는 대포에로드되도록해야합니다.)

The loading automaton will load all of the balls simultaneously.
(로드하는 자동 로봇은 모든 볼을 동시에로드합니다.)

The pipes are numbered from 0 to N-1.

(파이프는 0에서 N-1까지 번호가 지정됩니다.)

The initial positions of the backend mechanism are represented as an array with 1 and/or 0.

(백엔드 메커니즘의 초기 위치는 1 및 / 또는 0을 갖는 배열로 표시됩니다.)

Each element describes a cannon behind the pipe; the 0th element describe 0th pipe. 1 is a working cannon and 0 is a broken cannon.

(각 요소는 파이프 뒤의 캐논을 나타냅니다. 0 번째 요소는 0 번째 파이프를 나타냅니다. 1은 작동하는 대포이고 0은 부서진 대포입니다.)

You know the pipe numbers where the automaton will load cannonballs (sometimes it loads several cannonballs into one cannon).

(오토마타가 캐논볼을로드 할 파이프 번호를 알고 있습니다 (때로는 여러 개의 캐논볼을 하나의 캐논에로드합니다).)

Your goal is to find all the combinations that you can rotate the backend mechanism in a clockwise manner so that all of the cannonballs will be loaded into the working cannons.

(당신의 목표는 시계 방향으로 백엔드 메커니즘을 회전시킬 수있는 모든 조합을 찾아서 모든 캐논볼이 작업 대포에로드되도록하는 것입니다.)

Rotation is described as an integer - how many units you should rotate clockwise.

(회전은 정수로 설명됩니다. 시계 방향으로 회전해야하는 단위 수입니다.)

The result should be represented as a list of integers (variants) in the ascending order.

(결과는 오름차순으로 정수 (변형) 목록으로 표시되어야합니다.)

The case when you don't need to rotate are described as 0 (but don't forget about other variants).

(회전 할 필요가없는 경우는 0으로 표시됩니다 (단, 다른 변형은 잊지 마십시오).)

If it's not possible to find a solution, then return [].

(해결책을 찾을 수없는 경우 []를 반환하십시오.)

For example, the initial state is [1,0,0,0,1,1,0,1,0,0,0,1] and pipes numbers are [0,1].

(예를 들어, 초기 상태는 [1,0,0,0,1,1,0,1,0,0,0,1]이고 파이프 번호는 [0,1]입니다.)

If you rotate the mechanism by 1 or 8 units, then all balls which are be placed in the 0th and 1st pipes will be in cannons.

(너가 1 개 또는 8 단위에 의하여 기계 장치를 자전하면, 제 0 그리고 첫번째 관에서 두는 모든 공은 대포에있을 것이다.)

 

Input: Two arguments.

  • A initial state as a list with 1 and/or 0
  • Pipe numbers for cannonballs as a list of integers

두 가지 주장.

1 및 / 또는 0의리스트로서의 초기 상태

대포의 파이프 번호를 정수 목록으로 사용

 

Output: The rotating variants as a list of integers or an empty list.

           (정수 또는 빈 목록의 목록으로 회전하는 변형입니다.)

 

Example:

rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8]
rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == []
rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0]
rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5]

 

How it is used: This concept will acquaint you with circular data structures.

                     (이 개념은 원형 데이터 구조를 익히 게됩니다.)

 

Precondition:
3 ≤ len(state) < 100
all(0 ≤ n < len(state) for n in pipe_numbers)

 

A>

def rotate(state, pipe_numbers):
    pipe_numbers = sorted(set(pipe_numbers))
    # enumerate : 순서가 있는 자료형(리스트, 튜플, 문자열)을 입력으로 받아 인덱스 값을 포함하는 enumerate 객체를 돌려준다.
    #             인덱스 값이 필요할때 사용
    PipeSplit = [0] + [i + 1 for i, j in
                       enumerate(zip(pipe_numbers, pipe_numbers[1:]))
                       if j[0] == j[1]] + [len(pipe_numbers)]
    Cannons = [pipe_numbers[i[0]:i[1]] for i in zip(PipeSplit, PipeSplit[1:])]
    RotateCounter = []
    for i in Cannons:
        for j in range(len(state)):
            if len(i) == sum([k1 for k0, k1 in enumerate(state[-j:]+state[:-j]) if k0 in i]):
                RotateCounter.append(j)
    return RotateCounter

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1]) == [1, 8], "Example"
    assert rotate([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1], [0, 1, 2]) == [], "Mission impossible"
    assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [0, 4, 5]) == [0], "Don't touch it"
    assert rotate([1, 0, 0, 0, 1, 1, 0, 1], [5, 4, 5]) == [0, 5], "Two cannonballs in the same pipe"

 

O>

Pass: rotate([0,0,1,1,0,1,1,1,0,0,0,0,0,1,1,0,0,0,0,1,1,0,1,0,0,1,0,0,0,1,0,1,1,1,0,0,1,0,0,1,1,1,0,0,1,1,1,1,0,0,1,0,1,1,0,0,0,1,0,1,1,0,1,1,0,1,0,0,1,0,1,1,1,1,1,0,0,0,1,0,0,0,1,1,1,0,0,0,1,1,0,1,0,0,0,1,1,1,0,0],
[17,43,34,89,28,54])

 

Your result: [21,84]

 

S>

https://py.checkio.org

반응형

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

구구단 5단 출력 코드  (0) 2021.05.08
2 ~ 20 까지 세는 코드  (0) 2021.05.08
Python Learn the basics Quiz 112  (0) 2019.07.02
Python Learn the basics Quiz 111  (0) 2019.07.02
Python Learn the basics Quiz 110  (0) 2019.07.02