Skip to content

Instantly share code, notes, and snippets.

@neirbowj
Created March 5, 2021 01:11
Show Gist options
  • Save neirbowj/67e3660e3285fac9fbe528eb6d8b8f82 to your computer and use it in GitHub Desktop.
Save neirbowj/67e3660e3285fac9fbe528eb6d8b8f82 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def leftmost(bits):
"""
Generate 2-tuples in (ordinal, value) sequence according to the
order of the "leftmost" scheme specifed in
https://tools.ietf.org/html/rfc3531#section-3.1
for all numbers representable by `bits` bits.
"""
fmt = '{{:0{bits}b}}'.format(bits=bits)
for i, p in enumerate(range(2**bits)):
yield int(''.join(reversed(fmt.format(p))), base=2), i
for o, v in sorted(leftmost(5)):
print("allocate network {} to increment #{}".format(o, v))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment