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
|
#!/usr/bin/python3
'''
Week 174:
https://theweeklychallenge.org/blog/perl-weekly-challenge-174
Task #2: Permutation Ranking
You are given a list of integers with no duplicates, e.g. [0, 1, 2].
Write two functions, permutation2rank() which will take the list
and determine its rank (starting at 0) in the set of possible
permutations arranged in lexicographic order, and rank2permutation()
which will take the list and a rank number and produce just that
permutation.
'''
import unittest
import itertools
def permutation2rank(array):
array.sort()
return list(itertools.permutations(array))
def rank2permutation(array, rank):
p2r = permutation2rank(array)
return p2r[rank]
#
#
# Unit test class
class TestPermutationRanking(unittest.TestCase):
def test_permutation2rank(self):
exp = [ (0, 1, 2),
(0, 2, 1),
(1, 0, 2),
(1, 2, 0),
(2, 0, 1),
(2, 1, 0),
]
got = permutation2rank([1, 0, 2])
self.assertEqual(exp, got)
def test_rank2permutation(self):
exp = (0, 2, 1)
got = rank2permutation([1, 0, 2], 1)
self.assertEqual(exp, got)
unittest.main()
|