fork download
  1. from functools import cmp_to_key
  2.  
  3. def comp(x, y):
  4. return x - y if arr.count(x) == arr.count(y) else arr.count(y) - arr.count(x)
  5.  
  6. arr = [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 8, 8]
  7.  
  8. # Works because sorted is an iterable which doesn't modify the original array
  9. print(sorted(arr, key = cmp_to_key(comp)))
  10.  
  11. # Broken because this sorts the array in-place
  12. arr.sort(key = cmp_to_key(comp))
  13. print(arr)
Success #stdin #stdout #stderr 0.03s 42064KB
stdin
Standard input is empty
stdout
[1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 5, 5, 5, 5, 8, 8, 8, 2, 2, 7, 7, 0]
[0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 5, 5, 5, 5, 7, 7, 8, 8, 8]
stderr
Warning: cannot find your CPU L2 cache size in /proc/cpuinfo