-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
57 lines (48 loc) · 1.73 KB
/
main.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
# This script finds all unique permutations of a list of numbers
def find_perms(perm):
aset = set(range(len(perm)))
# print("set is {}".format(aset))
perm.find_perms(aset, [])
for each in perm.perms.values():
perm.permlist.append(each)
class permutations(list):
def __init__(self, alist):
super().__init__(alist)
self.perms = dict()
self.sort()
self.set = set(range(len(self)))
self.permlist = []
def add_soln(self, alist):
astr = ""
for each in alist:
astr = str(each) + "-" + astr
# print("adding solution {}".format(astr))
perm = self.perms.get(astr)
if perm == None:
self.perms[astr] = alist
def find_perms(self, aset, alist):
# print("fiding perms of {} alist {}".format(aset, alist))
# stopping case all elements are used
if aset == set():
self.add_soln(alist)
else:
for i in aset:
tset = aset.copy()
# remove the current element
tset.remove(i)
tlist = alist.copy()
tlist.append(self[i])
self.find_perms(tset, tlist)
if __name__ == "__main__":
alist = [1, 1, 2]
p = permutations(alist)
find_perms(p)
print("TEST#1 - {} unique permutations of {} are {}".format(len(p.permlist), alist, p.permlist))
alist = [1, 1, 2, 2]
p = permutations(alist)
find_perms(p)
print("TEST#2 - {} unique permutations of {} are {}".format(len(p.permlist), alist, p.permlist))
alist = [1, 1, 2, 3, 4 , 4]
p = permutations(alist)
find_perms(p)
print("TEST#3 - {} unique permutations of {} are {}".format(len(p.permlist), alist, p.permlist))