Skip to content

Instantly share code, notes, and snippets.

@orlp

orlp/encode.py Secret

Created January 12, 2017 00:16
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 orlp/326a0d79fece721c9c12f0ce900d46bd to your computer and use it in GitHub Desktop.
Save orlp/326a0d79fece721c9c12f0ce900d46bd to your computer and use it in GitHub Desktop.
def encode(combo, maxn):
base_offset = 1
s = 0
last = maxn + 1
for c in sorted(combo, reverse=True):
s += c*base_offset
base_offset *= last
last = c + 1
return s
def decode(n, num, maxn):
last = maxn + 1
r = []
for _ in range(num):
d = n % last
n //= last
last = d + 1
r.append(d)
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment