DSPython
00.03.03 — 25 juin 2012
Page principale
Paquetages
Classes
Fichiers
Liste des fichiers
Tout
Classes
Espaces de nommage
Fichiers
Fonctions
Variables
Pages
DSPython
partitions.py
Aller à la documentation de ce fichier.
1
#!/usr/bin/env python
2
# -*- coding: latin-1 -*-
3
##\package DSPython.partitions Partitions de naturels
4
#!!! Work in progress !!!
5
#
6
# Cf. \htmlonly <a href="http://www.opimedia.be/partitions/" target="_blank">
7
# <tt>http://www.opimedia.be/partitions/</tt></a>\endhtmlonly
8
9
##\file
10
# Partitions de naturels
11
# !!! Work in progress !!!
12
#
13
# Cf. \htmlonly <a href="http://www.opimedia.be/partitions/" target="_blank">
14
# <tt>http://www.opimedia.be/partitions/</tt></a>\endhtmlonly
15
16
# (c) Olivier Pirson --- DragonSoft
17
# http://www.opimedia.be/DS/
18
# Débuté le 20 août 2006
19
####################################
20
from
__future__
import
print_function
21
22
## Date du dernier changement pour ce module
23
VERSION =
'partitions --- 2010 April 12'
24
25
import
array
26
27
import
DSPython
28
import
DSPython.natural
as
natural
29
import
DSPython.integer
as
integer
30
31
32
33
# ###########
34
# Fonctions #
35
#############
36
## Compositions de n
37
def
compos
(n):
38
"""Renvoie la liste des compositions de n
39
40
Pre: n: naturel
41
42
Result: séquence ???
43
44
O(n) = ..."""
45
assert
DSPython.natural_is
(n), n
46
47
p = []
48
l = []
49
m = n
50
s = 0
51
#???print(n, ':', end='')
52
while
len(l) < n:
53
m = min(n - s, m)
54
#???print(m, end='')
55
if
m > 0:
56
#???print('+', end='')
57
s += m
58
l.append(m)
59
#???print(l, end='')
60
else
:
61
#???print('-', end='')
62
p.append(tuple(l))
63
if
len(l) > 1:
64
s -= l.pop()
65
l.append(l.pop() + 1)
66
s += 1
67
else
:
68
break
69
p.append(tuple(l))
70
#???print(p)
71
return
p
72
73
74
## Compositions de n de k termes
75
def
compos_k
(n, k):
76
"""Renvoie la liste des compositions de n de k termes
77
78
Pre: n: naturel
79
k: naturel ???
80
81
Result: séquence ???
82
83
O(n, k) = ..."""
84
assert
DSPython.natural_is
(n), n
85
assert
DSPython.natural_is
(k), k
86
87
if
0 < k <= n:
88
a = array.array(
'L'
, [1
for
i
in
range(k)])
# composition de k termes
89
a[0] = n - k + 1
90
for
i
in
range(n - k):
91
pass
##???
92
return
[tuple(a)]
93
elif
k == 0 == n:
94
return
[()]
95
else
:
96
return
[]
97
98
return
(integer.binom(n - 1, k - 1)
if
n >= k
99
else
0)
100
101
102
## Nombre de compositions de n
103
def
compos_nb
(n):
104
"""Renvoie le nombre de compositions de n
105
106
Pre: n: naturel
107
108
Result: naturel
109
110
O(n) = ..."""
111
assert
DSPython.natural_is
(n), n
112
113
return
(natural.pow2(n - 1)
if
n > 0
114
else
1)
115
116
117
118
# ######\cond MAINTEST
119
# Main #
120
########
121
if
__name__ ==
'__main__'
:
122
def
main_test
():
123
"""Test du module"""
124
import
sys
125
126
import
DSPython.debug
as
debug
127
128
debug.test_begin(VERSION, __debug__)
129
130
print(
'compos()...'
, end=
''
); sys.stdout.flush()
131
assert
compos
(0) == [()],
compos
(0)
132
assert
compos
(1) == [(1,)],
compos
(1)
133
#assert compos(2) == [(2,), (1, 1)], compos(2)
134
#assert compos(3) == [(3,), (2, 1), (1, 2), (1, 1, 1)], compos(3)
135
print(
'???'
, end=
''
)
136
print(
'ok'
); sys.stdout.flush()
137
138
139
print(
'compos_k()...'
, end=
''
); sys.stdout.flush()
140
assert
compos_k
(0, 0) == [()],
compos_k
(0, 0)
141
for
n
in
range(1, 100):
142
assert
compos_k
(n, 0) == [], (n,
compos_k
(n, 0))
143
assert
compos_k
(n, 1) == [(n, )], (n,
compos_k
(n, 1))
144
#???assert compos_k(3, 2) == [(2, 1), (1, 2)], compos_k(3, 2)
145
print(
'???'
, end=
''
)
146
print(
'ok'
); sys.stdout.flush()
147
148
149
## print('compos_k_nb()...', end=''); sys.stdout.flush()
150
## assert compos_k_nb(0, 0) == 1, compos_k_nb(0, 0)
151
## for n in range(1, 100):
152
## assert compos_k_nb(n, 0) == 0, (n, compos_k_nb(n, 0))
153
## assert compos_k_nb(n, 1) == 1, (n, compos_k_nb(n, 1))
154
## assert compos_k_nb(n, n) == 1, (n, compos_k_nb(n, n))
155
## for k in range(1, 50):
156
## assert compos_k_nb(n, n+k) == 0, (n, k, compos_k_nb(n, n+k))
157
## assert compos_k_nb(3, 2) == 2, compos_k_nb(3, 2)
158
## assert compos_k_nb(4, 2) == 3, compos_k_nb(4, 2)
159
## assert compos_k_nb(4, 3) == 3, compos_k_nb(4, 3)
160
## for n in range(1, 50):
161
## s = 0
162
## for k in range(n+1):
163
## s += compos_k_nb(n, k)
164
## assert s == compos_nb(n), (n, s, compos_nb(n))
165
## print('???', end='')
166
## print('ok'); sys.stdout.flush()
167
168
169
print(
'compos_nb()...'
, end=
''
); sys.stdout.flush()
170
assert
compos_nb
(0) == 1,
compos_nb
(0)
171
assert
compos_nb
(1) == 1,
compos_nb
(1)
172
assert
compos_nb
(2) == 2,
compos_nb
(2)
173
assert
compos_nb
(3) == 4,
compos_nb
(3)
174
for
n
in
range(1, 100):
175
assert
compos_nb
(n) == 2**(n - 1), (n,
compos_nb
(n))
176
print(
'???'
, end=
''
)
177
print(
'ok'
); sys.stdout.flush()
178
debug.test_end()
179
180
main_test
()
181
##\endcond MAINTEST