Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (52 sloc) 1.22 KB
#!/usr/bin/python
def perm(x,last=0):
if len(x)<=last+1:
yield x
else:
for t in perm(x,last+1):
yield t
for i in range(last+1,len(x)):
x[last],x[i]=x[i],x[last]
for t in perm(x,last+1):
yield t
x[last],x[i]=x[i],x[last]
def s_perm(x,last=0):
if len(x)<=last+1:
yield x
else:
for t in s_perm(x,last+1):
yield t
for i in range(last+1,len(x)):
x[last],x[i]=x[i],x[last]
for t in s_perm(x,last+1):
yield t
x[last]=-x[last]
for t in s_perm(x,last+1)
x[last],x[i]=x[i],-x[last]
def sgn(x):
if x & 1:
return(-1)
else:
return(1)
def signed_perm(x):
m= (01 << (len(x)))
for t in perm(x):
for b in range(m):
s=t[:]
j,k=b,0
while j>0:
s[k]=sgn(j)*s[k]
j=j>>1
k+=1
yield s
S=[x for x in signed_perm([1,2,3])]
print len(S)
for x in S:
for i in x:
print i,
print
def fact(n):
if n==0:
return(1)
else:
return n*fact(n-1)
You can’t perform that action at this time.