class OrderMaker:
def __init__(self, n=8):
self.n = n
- self.a = []
- self.u = []
- self.s = []
- for x in range(0,n):
- self.u.append(False)
- self.go()
-
- def go(self):
- from copy import deepcopy
- if len(self.s) == self.n:
- self.a.append(deepcopy(self.s))
- else:
- for x in range(0,self.n):
- if self.u[x]: continue
- self.s.append(x)
- self.u[x] = True
- self.go()
- self.u[x] = False
- self.s.pop()
-
- def order(self):
- return self.a
+ self.make_factorials(n)
+
+ def make_factorials(self, n):
+ self.factorial = []
+ a = 1
+ for i in range(1,n+1):
+ self.factorial.append(a)
+ a *= i
+
+ def order(self, index):
+ used = []
+ for i in range(0,self.n):
+ used.append(i)
+ i = self.n-1
+ j = 0
+ res = []
+ while i >= 0:
+ a = index/self.factorial[i]
+ index %= self.factorial[i]
+ res.append(a+1)
+ i -= 1
+ j += 1
+ for i in range(0,self.n):
+ tmp = used[res[i]-1]
+ for j in range(res[i],self.n):
+ used[j-1] = used[j]
+ res[i] = tmp
+ return res
+
+ def __getitem__(self, i):
+ return self.order(i)
class GrayIdler(Idler):
def __init__(self, v, one=None, zero=None, reorder=0):
self.reorder = reorder
global orderings
if not orderings:
- orderings = OrderMaker().order()
+ orderings = OrderMaker()
def next(self):
output = self.do_next_state()