diff options
Diffstat (limited to 'challenge-233/steven-wilson/python/ch-2.py')
| -rw-r--r-- | challenge-233/steven-wilson/python/ch-2.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-233/steven-wilson/python/ch-2.py b/challenge-233/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..7bd69c1e7e --- /dev/null +++ b/challenge-233/steven-wilson/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def frequencySort(elements): + """Return list sorted by frequency then decending order + >>> frequencySort([1, 1, 2, 2, 2, 3]) + [3, 1, 1, 2, 2, 2] + >>> frequencySort([2, 3, 1, 3, 2]) + [1, 3, 3, 2, 2] + >>> frequencySort([-1, 1, -6, 4, 5, -6, 1, 4, 1]) + [5, -1, 4, 4, -6, -6, 1, 1, 1] + """ + elemCount = Counter(elements) + return sorted(elements, key=lambda x: (elemCount[x], -x)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + |
