Last active
July 5, 2020 14:47
-
-
Save darius/4733be9d85494708c7182708e763bf3f to your computer and use it in GitHub Desktop.
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
""" | |
Problem from David Albert | |
'bumps' idea from Michael Arntzenius & Eugene Ha | |
""" | |
def repr_register_bits(bits): | |
return '/'.join(repr_bits('D', bits & 0xFF) + repr_bits('A', bits >> 8)) | |
def repr_bits(label, bits): | |
bits = [(bits>>i) & 1 for i in range(8)] | |
bumps = [next_b - b for b,next_b in zip([0]+bits, bits+[0])] | |
return [repr_range(label, r) for r in zip(where(1, bumps), where(-1, bumps))] | |
def repr_range(label, (start, after)): | |
i, j = start, after-1 | |
return '%s%d' % (label, i) if i==j else '%s%d-%s%d' % (label, i, label, j) | |
def where(target, things): | |
return [i for i,thing in enumerate(things) if thing == target] | |
## repr_register_bits(0x1221) | |
#. 'D0/D5/A1/A4' | |
## repr_register_bits(0xfc2f) | |
#. 'D0-D3/D5/A2-A7' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment