Procrastination pythonesque
Freedog demandait un algorithme pour calculer les partitions dont parle eljj dans son dernier billet. Comme j’ai du temps à perdre (…), j’ai programmé ça vite fait en python :
import copy
def partition(n):
list=[]
if (n==1):
list.append([1])
return list
list.append([n])
list_previous=partition(n-1)
for l in list_previous:
list_increment=copy.deepcopy(l)
list_increment.append(1)
list_increment.sort()
if not list_increment in list:
list.append(list_increment)
for i in xrange(len(l)):
list_addition=copy.deepcopy(l)
list_addition[i]=list_addition[i]+1
list_addition.sort()
if not list_addition in list:
list.append(list_addition)
return list
l=partition(9)
print l
print len(l)
Qui renvoie ici :
[[9], [1, 8], [1, 1, 7], [2, 7], [1, 1, 1, 6], [1, 2, 6], [3, 6], [1, 1, 1, 1, 5], [1, 1, 2, 5], [2, 2, 5], [1, 3, 5], [4, 5], [1, 1, 1, 1, 1, 4], [1, 1, 1, 2, 4], [1, 2, 2, 4], [1, 1, 3, 4], [2, 3, 4], [1, 4, 4], [1, 1, 1, 1, 1, 1, 3], [1, 1, 1, 1, 2, 3], [1, 1, 2, 2, 3], [1, 1, 1, 3, 3], [2, 2, 2, 3], [1, 2, 3, 3], [3, 3, 3], [1, 1, 1, 1, 1, 1, 1, 2], [1, 1, 1, 1, 1, 2, 2], [1, 1, 1, 2, 2, 2], [1, 2, 2, 2, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1]]
30
ce qui a l’air assez raisonnable. Cela me permet de découvrir l’existence de la balise html <pre> , et de la tester. Je suis maintenant fin prêt pour écrire plein de choses sur python et son utilisation … en biologie
Aucun billet similaire

June 15th, 2009 at 16:52
(Commentaire de chieur)
Pour afficher du code, on utilise la balise
pour du code "inline" comme tu l'as fait pour la sortie du programme, etdans une balise pour du "block".Par exemple
import copy
def partition(n):
Et voilà comment indexe encore mieux…
[Reply]
June 15th, 2009 at 17:32
Ah mais non, c’est ce que j’avais essayé au départ, mais cela avait mis le bordel dans l’indentation (cruciale en python)…
[Reply]
June 15th, 2009 at 18:45
pas mal … mais peut mieux faire : http://code.activestate.com/recipes/218332/
la distribution Python des scientifiques c’est http://www.pythonxy.com . Avec ça plus besoin de Matlab. (et c’est en français!)
[Reply]
June 15th, 2009 at 18:54
Le fameux “yield” qui change tout …
[Reply]
June 16th, 2009 at 11:45
Le fameux “yield” qui change tout …
Effectivement, un album clef dans la discographie de Pearl Jam. Peut-être le plus abouti.
[Reply]
June 17th, 2009 at 01:43
Ok, bon, la prochaine fois je me la fermerai. (j’étais sûr de l’avoir déjà fait, et après examination du code, il y a une bidouille dans le CSS pour avoir une belle sémantique). Mea culpa.
[Reply]
August 8th, 2009 at 22:52
C’est drÃ?´le! pour ma part je connais quelqu’un qui a vu le spectacle de ses yeux donc je crois que c’est vrai
[Reply]