본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 57

반응형

Q>

Let's try some sorting.

(정렬을 해보죠.)

Here is an array with the specific rules.

(여기에 특정 규칙이있는 배열이 있습니다.)

The array (a tuple) has various numbers.

(배열 (튜플)에는 다양한 숫자가 있습니다.)

You should sort it, but sort it by absolute value in ascending order.

(그것을 정렬해야하지만, 오름차순으로 절대 값으로 정렬하십시오.)

For example, the sequence (-20, -5, 10, 15) will be sorted like so: (-5, 10, 15, -20).

(예를 들어, 순서 (-20, -5, 10, 15)는 (-5, 10, 15, -20)와 같이 정렬됩니다.)

Your function should return the sorted list or tuple.

(함수는 정렬 된 목록 또는 튜플을 반환해야합니다.)

 

Precondition: The numbers in the array are unique by their absolute values.

                   (배열의 숫자는 절대 값으로 고유합니다.)

 

Input: An array of numbers , a tuple..

        (숫자의 배열, 튜플 ..)

 

Output: The list or tuple (but not a generator) sorted by absolute values in ascending order.

           (오름차순으로 절대 값별로 정렬 된 목록 또는 튜플 (하지만 생성기는 아님).)

 

Addition: The results of your function will be shown as a list in the tests explanation panel.

             (기능 결과가 테스트 설명 패널에 목록으로 표시됩니다.)

 

Example:

checkio((-20, -5, 10, 15)) == [-5, 10, 15, -20] # or (-5, 10, 15, -20)
checkio((1, 2, 3, 0)) == [0, 1, 2, 3]
checkio((-1, -2, -3, 0)) == [0, -1, -2, -3]

 

How it is used: Sorting is a part of many tasks, so it will be useful to know how to use it.

                     (정렬은 많은 작업의 일부이므로이를 사용하는 방법을 알고 있으면 유용합니다.)

 

Precondition: len(set(abs(x) for x in array)) == len(array)

                   0 < len(array) < 100
                   all(isinstance(x, int) for x in array)
                   all(-100 < x < 100 for x in array)

 

A>

def checkio(numbers_array: tuple) -> list:
    # sorted는 오름차순정렬 이므로 key에 abs를 지정하여 내림차순 정렬
    # abs : 절대값 구하는 함수
    return tuple(sorted(numbers_array, key=abs))

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    print('Example:')
    print(list(checkio((-20, -5, 10, 15))))

    def check_it(array):
        if not isinstance(array, (list, tuple)):
            raise TypeError("The result should be a list or tuple.")
        return list(array)

    assert check_it(checkio((-20, -5, 10, 15))) == [-5, 10, 15, -20], "Example"  # or (-5, 10, 15, -20)
    assert check_it(checkio((1, 2, 3, 0))) == [0, 1, 2, 3], "Positive numbers"
    assert check_it(checkio((-1, -2, -3, 0))) == [0, -1, -2, -3], "Negative numbers"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

 

O>

Example:
[-5, 10, 15, -20]
Coding complete? Click 'Check' to review your tests and earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형

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

Python Learn the basics Quiz 59  (0) 2019.06.12
Python Learn the basics Quiz 58  (0) 2019.06.12
Python Learn the basics Quiz 56  (0) 2019.06.12
Python Learn the basics Quiz 55  (0) 2019.06.11
Python Learn the basics Quiz 54  (0) 2019.06.11