Skip to content

Instantly share code, notes, and snippets.

@WizzyGeek
Last active April 21, 2024 01:35
Show Gist options
  • Save WizzyGeek/6bf96891be80ee58cfea32204918f57c to your computer and use it in GitHub Desktop.
Save WizzyGeek/6bf96891be80ee58cfea32204918f57c to your computer and use it in GitHub Desktop.
Fast and compact digit permuation invariant / efficient digit counting
ORD0 = ord('0') - 1
def num_to_bs(num):
bs = 0
for i in str(num):
s = 1 << ((ord(i) - ORD0) * 4)
l = bs & s
bs += (s >> 4)
if l != (bs & s): raise RuntimeError
return bs
_O = ORD0 + 1
def _n2b(n):
return sum(map(lambda c: 1 << ((ord(c) - 48) * 4), str(n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment