Skip to content

Instantly share code, notes, and snippets.

@RickyCook
Forked from forsakendaemon/scramble.py
Last active July 27, 2016 04:23
Show Gist options
  • Save RickyCook/4b62bb04f3cd965398103db13dbe92d6 to your computer and use it in GitHub Desktop.
Save RickyCook/4b62bb04f3cd965398103db13dbe92d6 to your computer and use it in GitHub Desktop.
GROUP_BITS = 31
PRIME = 7
def ddd(index):
"""
Examples:
>>> ddd(111)
'00*00*0001***0*0*001****0**0**'
"""
bitstring = bin(int(index))[2:].zfill(GROUP_BITS - 1)
return encode(bitstring, GROUP_BITS, PRIME)
def encode(y, s, p):
l = ["*"] * (s - 1)
i = 1
tmp = y[0]
for t in range(s):
l[i - 1] = tmp
tmp = y[i - 1]
i = i * p % s
return ''.join(l)
def inv(s, p):
for x in range(s):
if x * p % s == 1:
return x
return None
def decode(y, s, p):
return encode(y, s, inv(s, p))
def arr2int(arr):
return int(''.join(arr), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment