본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 75

반응형

Q>

Nicola likes to categorize all sorts of things.

(Nicola는 모든 종류의 것들을 분류하기를 좋아합니다.)

He categorized a series of numbers and as the result of his efforts, a simple sequence of numbers became a deeply-nested list.

(그는 일련의 숫자를 범주화하고 그의 노력의 결과로 단순한 숫자의 연속이 깊이 중첩 된 목록이되었습니다.)

Sophia and Stephan don't really understand his organization and need to figure out what it all means.

(소피아와 스테판은 자신의 조직을 정말로 이해하지 못하고 그것이 무엇을 의미하는지 알아야합니다. )

They need your help to understand Nikolas crazy list.

(그들은 Nikolas 미친 명부를 이해하는 당신의 도움이 필요합니다.)

There is a list which contains integers or other nested lists which may contain yet more lists and integers which then…

(더 많은 목록과 정수를 포함 할 수있는 정수 또는 다른 중첩 목록을 포함하는 목록이 있습니다.)

you get the idea.

(그러면 다음과 같은 아이디어가 나타납니다.)

You should put all of the integer values into one flat list.

(모든 정수 값을 하나의 단순 목록에 넣어야합니다.)

The order should be as it was in the original list with string representation from left to right.

(순서는 왼쪽에서 오른쪽으로의 문자열 표현을 사용하여 원래 목록에 있었던 순서와 같아야합니다.)

We need to hide this program from Nikola by keeping it small and easy to hide. Because of this, 

(우리는이 프로그램을 작고 쉽게 숨겨서 Nikola에서 숨길 필요가 있습니다. 이것 때문에,)

your code should be shorter than 140 characters (with whitespaces).

(코드는 공백없이 140 자보다 짧아야합니다.)

 

Input data: A nested list with integers.

               (정수가있는 중첩 목록입니다.)

 

Output data: The one-dimensional list with integers.

                  (정수가있는 1 차원 목록입니다.)

 

Example:

flat_list([1, 2, 3]) == [1, 2, 3]
flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4]
flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7]
flat_list([-1, [1, [-2], 1], -1]) == [-1, 1, -2, 1, -1]

 

How it is used: This concept is useful for parsing and analyzing files with complex structures and the task challenges your creativity in writing short code.

(이 개념은 복잡한 구조의 파일을 파싱하고 분석하는 데 유용하며 작업을 통해 창의력에 짧은 코드를 작성해야합니다.)

 

Precondition: 0 ≤ |array| ≤ 100
                   ∀ x ∈ array : -232 < x < 232 or x is a list
                   depth < 10

 

A>

def flat_list(array):
    return sum(([x] if not isinstance(x, list) else flat_list(x) for x in array), [])

if __name__ == '__main__':
    assert flat_list([1, 2, 3]) == [1, 2, 3], "First"
    assert flat_list([1, [2, 2, 2], 4]) == [1, 2, 2, 2, 4], "Second"
    assert flat_list([[[2]], [4, [5, 6, [6], 6, 6, 6], 7]]) == [2, 4, 5, 6, 6, 6, 6, 6, 7], "Third"
    assert flat_list([-1, [1, [-2], 1], -1]) == [-1, 1, -2, 1, -1], "Four"
    print('Done! Check it')

 

O>

Done! Check it

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

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

Python Learn the basics Quiz 77  (0) 2019.06.20
Python Learn the basics Quiz 76  (0) 2019.06.20
Python Learn the basics Quiz 74  (0) 2019.06.20
Python Learn the basics Quiz 73  (0) 2019.06.19
Python Learn the basics Quiz 72  (0) 2019.06.19