Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Classic solution in 3rd party category for Sort Array by Element Frequency by PistoleroAlan
from typing import Iterable
from collections import Counter
import numpy as np
def frequency_sort(items: list[str | int]) -> Iterable[str | int]:
itemssort=[]
for x,f in Counter(items).most_common():
itemssort.extend(np.repeat(x,f))
return itemssort
print("Example:")
print(list(frequency_sort([4, 6, 2, 2, 6, 4, 4, 4])))
# 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!")
Aug. 25, 2023
Comments: