Skip to content

Instantly share code, notes, and snippets.

@scarecrow1123
Created February 20, 2016 10:07
Show Gist options
  • Save scarecrow1123/85679b2da10603ab15a3 to your computer and use it in GitHub Desktop.
Save scarecrow1123/85679b2da10603ab15a3 to your computer and use it in GitHub Desktop.
Sort a list based on the number of set bits in each digit
def count_set_bits(i):
return bin(i).count('1')
def sort_set_bits(lst):
"""sorts a list based on the number of set bits in each digit"""
return sorted(lst, key=count_set_bits)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment