Light Mode
Dark Mode
Sort arr by elem frequency: fail on final assert
import collections

def frequency_sort(items):
    if items == []:
        return items

    counts = collections.Counter(items)  
    for i in counts:
        if counts[i] > 1:
            new_list = sorted(items, key=lambda x: (counts[x], x), reverse=True)
            return new_list
        else:
            return items

Hi all! This code from "Sort Array by Element Frequency" fails on the final assert, where frequency_sort([1,2,2,1]). It returns [2,2,1,1], instead required [1,1,2,2] and I don't know why it happened particularly here and how to fix that. Thanks!

  • sort_array_by_elenent_frequency
Created: Aug. 3, 2019, 5:25 p.m.
Updated: Aug. 27, 2019, 4:07 p.m.
0
9
User avatar
BallV