forked from jfinkels/PADS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_permutations.py
59 lines (51 loc) · 2.15 KB
/
test_permutations.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
import unittest
from pads.permutations import Involutions
from pads.permutations import PlainChanges
from pads.permutations import SteinhausJohnsonTrotter
class PermutationTest(unittest.TestCase):
def testChanges(self):
"""Do we get the expected sequence of changes for n=3?"""
self.assertEqual(list(PlainChanges(3)),[1,0,1,0,1])
def testLengths(self):
"""Are the lengths of the generated sequences factorial?"""
f = 1
for i in range(2,7):
f *= i
self.assertEqual(f,len(list(SteinhausJohnsonTrotter(i))))
def testDistinct(self):
"""Are all permutations in the sequence different from each other?"""
for i in range(2,7):
s = set()
n = 0
for x in SteinhausJohnsonTrotter(i):
s.add(tuple(x))
n += 1
self.assertEqual(len(s),n)
def testAdjacent(self):
"""Do consecutive permutations in the sequence differ by a swap?"""
for i in range(2,7):
last = None
for p in SteinhausJohnsonTrotter(i):
if last:
diffs = [j for j in range(i) if p[j] != last[j]]
self.assertEqual(len(diffs),2)
self.assertEqual(p[diffs[0]],last[diffs[1]])
self.assertEqual(p[diffs[1]],last[diffs[0]])
last = list(p)
def testListInput(self):
"""If given a list as input, is it the first output?"""
for L in ([1,3,5,7], list('zyx'), [], [[]], list(range(20))):
self.assertEqual(L,next(SteinhausJohnsonTrotter(L)))
def testInvolutions(self):
"""Are these involutions and do we have the right number of them?"""
telephone = [1,1,2,4,10,26,76,232,764]
for n in range(len(telephone)):
count = 0
sorted = list(range(n))
invs = set()
for p in Involutions(n):
self.assertEqual([p[i] for i in p],sorted)
invs.add(tuple(p))
count += 1
self.assertEqual(len(invs),count)
self.assertEqual(len(invs),telephone[n])