Skip to content

Instantly share code, notes, and snippets.

@x-or
Created July 2, 2014 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-or/0c263fc5865a8bfb7259 to your computer and use it in GitHub Desktop.
Save x-or/0c263fc5865a8bfb7259 to your computer and use it in GitHub Desktop.
Digital Reverse in Python
def digital_reverse(n, length, base):
r = 0
for _ in xrange(length):
r = base*r + n % base
n /= base
return r
assert digital_reverse(123, 3, 10) == 321
assert digital_reverse(54321, 5, 10) == 12345
assert digital_reverse(0x123, 3, 16) == 0x321
assert digital_reverse(0x54321, 5, 16) == 0x12345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment