Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in 3rd party category for Sort Array by Element Frequency by Pavellver
from typing import Iterable
import pandas as pd
def frequency_sort(items: list[str | int]) -> Iterable[str | int]:
result = []
m = dict(pd.value_counts(items))
print('Dictionary from list: ', m)
for i in items:
if items.count(i) == 1:
return items
elif items.count(i) == 0:
return items
else:
for item in m:
for i1 in range(0, m[item]):
result.append(item)
return result
return items
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!")
Feb. 13, 2023
Comments: