Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
1
Indexable
Never
#Sort the given list so that its elements should be grouped and those groups end up in the decreasing frequency order, that is, the #number of times element appears in list. If two elements have the same frequency, their groups should end up in the same order as #the first appearance of element in the list.

# from typing import Iterable
# 
# 
# def frequency_sort(items: list[str | int]) -> Iterable[str | int]:
#     return sorted(items, key= lambda a: (items.count(a), - items.index(a)), reverse=True)
# 
# 
# print("Example:")
# print(list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])))
# frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])
# frequency_sort(['6', '1', '3', '3', '51', '6', '6'])
# #
# # These "asserts" are used for self-checking
# assert list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 6, 6, 2, 2]
# assert list(frequency_sort([4, 6, 2, 2, 2, 6, 4, 4, 4])) == [4, 4, 4, 4, 2, 2, 2, 6, 6]
# 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("The mission is done! Click 'Check Solution' to earn rewards!")