Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Frequency Sorting by vlad.bezden
"""Frequency Sorting
https://py.checkio.org/en/mission/frequency-sorting/
Your mission is to sort the list by the frequency of numbers included in it.
If a few numbers have an equal frequency -
they should be sorted according to their natural order.
For example: [5, 2, 4, 1, 1, 1, 3] ==> [1, 1, 1, 2, 3, 4, 5]
Input: Chaotic list of numbers.
Output: The list of numbers in which they are sorted by their frequency.
Precondition:
array length <= 100
max number <= 100
"""
from typing import List
def frequency_sorting(numbers: List[int]) -> List[int]:
return sorted(sorted(numbers), key=numbers.count, reverse=True)
if __name__ == "__main__":
assert frequency_sorting([1, 2, 3, 4, 5]) == [1, 2, 3, 4, 5], "Already sorted"
assert frequency_sorting([3, 4, 11, 13, 11, 4, 4, 7, 3]) == [
4,
4,
4,
3,
3,
11,
11,
7,
13,
], "Not sorted"
assert frequency_sorting([99, 99, 55, 55, 21, 21, 10, 10]) == [
10,
10,
21,
21,
55,
55,
99,
99,
], "Reversed"
print("PASSED!!!")
July 24, 2020