Skip to content

Instantly share code, notes, and snippets.

@bebraw
Created August 22, 2010 12:15
Show Gist options
  • Save bebraw/543707 to your computer and use it in GitHub Desktop.
Save bebraw/543707 to your computer and use it in GitHub Desktop.
def count_ones(hex_str):
def count_char(n):
mapping = lambda m: (m + 1) / 2
return mapping(n % 4) + mapping(n / 4)
return sum([count_char(int(char, 16)) for char in hex_str])
assert count_ones('aac8') == 7
assert count_ones('0') == 0
assert count_ones('f') == 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment