-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutator.py
75 lines (57 loc) · 1.61 KB
/
Permutator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
class DigitCounter():
def __init__(self,NDigits, MaxDigits=None):
self.NDigits=NDigits
if MaxDigits:
self.MaxDigits=MaxDigits
else:
self.MaxDigits=[9]*NDigits
self.Digits=[0]*NDigits
self.Done=False
def Increment(self):
if self.Done:
return False
for i in xrange(self.NDigits-1,-1,-1):
if self.Digits[i]>=self.MaxDigits[i]:
if i==0:
self.Done=True
self.Digits[i]=0
else:
self.Digits[i]+=1
break
return True
def Reset(self):
self.Digits=[0]*self.NDigits
self.Done=False
class Permutator():
def __init__(self,Params):
self.Params=Params
DigitMaxs=[0]*len(Params.keys())
i=0
self.keys=Params.keys()
self.keys.sort()
for p in self.keys:
DigitMaxs[i]=len(Params[p])-1
i+=1
self.Digits=DigitCounter(len(Params.keys()),DigitMaxs)
self.Done=False
def GetPermutation(self):
out={}
i=0
for p in self.keys:
sys.stdout.flush()
out[p]=self.Params[p][self.Digits.Digits[i]]
i+=1
if self.Digits.Increment():
return out
else:
return False
def Permutations(self):
self.Digits.Reset()
res=True
out=[]
while res:
res=self.GetPermutation()
out+=[res]
self.Digits.Reset()
return out[:-1]