Skip to content

Instantly share code, notes, and snippets.

@raullenchai
Created July 2, 2012 17:14
Show Gist options
  • Save raullenchai/3034378 to your computer and use it in GitHub Desktop.
Save raullenchai/3034378 to your computer and use it in GitHub Desktop.
Compute the Inverse of a Permutation in F_2^n
import math
def isPerm(x):
if math.log(len(x), 2)% 1 != 0:
return False
for i in range(len(x)):
try:
x.index(i)
except ValueError:
return False
return True
def inv(x):
finv = [ 0 for i in range(len(x)) ]
for i in range(len(x)):
finv[f[i]] = i;
return finv
if __name__ == '__main__':
f = [1,3,2,0]
if isPerm(f):
print inv(f)
else:
print 'Not a Permutation!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment