Last active
April 21, 2024 01:35
-
-
Save WizzyGeek/6bf96891be80ee58cfea32204918f57c to your computer and use it in GitHub Desktop.
Fast and compact digit permuation invariant / efficient digit counting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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