-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitions.py
36 lines (32 loc) · 905 Bytes
/
partitions.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
# PARTITION FUNTIONS
# Both ripped from overflow questions, slightly modified to
# return tuples with numbers in reverse order.
# The function quick_partition is much much better.
def partition(number):
answer = set()
answer.add((number, ))
for x in range(1, number):
for y in partition(number - x):
answer.add(tuple(sorted((x, ) + y, reverse=True)))
return answer
def quick_partition(n):
a = [0 for i in range(n + 1)]
k = 1
y = n - 1
while k != 0:
x = a[k - 1] + 1
k -= 1
while 2 * x <= y:
a[k] = x
y -= x
k += 1
l = k + 1
while x <= y:
a[k] = x
a[l] = y
yield tuple(sorted(a[:k + 2], reverse=True))
x += 1
y -= 1
a[k] = x + y
y = x + y - 1
yield tuple(sorted(a[:k + 1], reverse = True))