-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
102 lines (86 loc) · 2.34 KB
/
util.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""
Various combinatorial utilities that don't fit elsewhere.
"""
__all__ = [
'compositions',
'inversions',
'partitions',
]
# ---------------------------------------------------------
import itertools as it
import numpy as np
import operator
from collections import defaultdict, Counter, namedtuple
from path import *
from perm import *
# ---------------------------------------------------------
def compositions(n):
r"""
Iterator for the compositions of the integer `n`.
>>> sorted(list(compositions(3)))
[(1, 1, 1), (1, 2), (2, 1), (3,)]
>>> len(list(compositions(8)))
128
"""
if n == 0:
yield ()
return
for head in range(1, n+1):
for tail in compositions(n-head):
yield (head,) + tail
def partitions(n, bound=None):
r"""
Iterator for the partitions of the integer `n`.
Optional argument `bound` is an upper bound on the part sizes.
>>> sorted(list(partitions(3)))
[(1, 1, 1), (2, 1), (3,)]
>>> len(list(partitions(8)))
22
>>> sorted(list(partitions(4, 2)))
[(1, 1, 1, 1), (2, 1, 1), (2, 2)]
"""
if n == 0:
yield ()
return
if bound is None or bound > n:
bound = n
for head in range(1, bound+1):
for tail in partitions(n-head, head):
yield (head,) + tail
# ---------------------------------------------------------
def inversions(path, perm):
r"""
Compute the number of path-inversions for a given permutation.
>>> inversions((0, 0, 0), (0, 1, 2))
0
>>> inversions((0, 0, 0), (0, 2, 1))
1
>>> inversions((0, 0, 0), (1, 0, 2))
1
>>> inversions((0, 0, 0), (1, 2, 0))
2
>>> inversions((0, 0, 0), (2, 0, 1))
2
>>> inversions((0, 0, 0), (2, 1, 0))
3
>>> inversions((0, 0, 1), (0, 1, 2))
0
>>> inversions((0, 0, 1), (0, 2, 1))
1
>>> inversions((0, 0, 1), (1, 0, 2))
1
>>> inversions((0, 0, 1), (1, 2, 0))
1
>>> inversions((0, 0, 1), (2, 0, 1))
1
>>> inversions((0, 0, 1), (2, 1, 0))
2
"""
return sum((perm[i] > perm[j]) for i, j in boxes_under_path(path))
# ---------------------------------------------------------
if __name__ == '__main__':
import doctest
doctest.testmod()
# ---------------------------------------------------------