본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 74

반응형

Q>

Sort the given iterable so that its elements end up in the decreasing frequency order, that is, the number of times they appear in elements.

(주어진 iterable을 정렬하여 요소가 감소하는 빈도 순서로 끝나도록합니다. 즉 요소에 나타나는 횟수입니다.)

If two elements have the same frequency, they should end up in the same order as the first appearance in the iterable.

(두 요소의 빈도가 같으면 iterable의 첫 번째 모양과 동일한 순서로 끝나야합니다.)

 

Input: Iterable

         (반복 가능)

 

Output: Iterable

           (반복 가능)

 

Example:

frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]) == [4, 4, 4, 4, 6, 6, 2, 2]
frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob']) == ['bob', 'bob', 'bob', 'carl', 'alex']

 

Precondition: elements can be ints or strings

                   (요소는 int 또는 문자열 일 수 있습니다.)

 

The mission was taken from Python CCPS 109 Fall 2018. It's being taught for Ryerson Chang School of Continuing Education by Ilkka Kokkarinen

(선교사는 Python CCPS 109 Fall 2018에서 가져 왔습니다. Ryerson Chang 평생 교육 학교 (Ilkka Kokkarinen)가 가르치고 있습니다.)

 

A>

def frequency_sort(items):
    # your code here
    return sorted(items, key=lambda x: (-items.count(x), items.index(x)))

if __name__ == '__main__':
    print("Example:")
    print(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4]))

    # These "asserts" are used for self-checking and not for an auto-testing
    assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 6, 6, 2, 2]
    assert list(frequency_sort(['bob', 'bob', 'carl', 'alex', 'bob'])) == ['bob', 'bob', 'bob', 'carl', 'alex']
    assert list(frequency_sort([17, 99, 42])) == [17, 99, 42]
    assert list(frequency_sort([])) == []
    assert list(frequency_sort([1])) == [1]
    print("Coding complete? Click 'Check' to earn cool rewards!")

 

O>

Example:
[4, 4, 4, 4, 6, 6, 2, 2]
Coding complete? Click 'Check' to earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org

반응형

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

Python Learn the basics Quiz 76  (0) 2019.06.20
Python Learn the basics Quiz 75  (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
Python Learn the basics Quiz 71  (0) 2019.06.17