pff, moi non plus, toujours pas de technique de dénombrement
pour le 9/3 de Lostounet
123
145
167
189
246
278
347
489
est différent et a aussi 8 solutions
import random
def Cnk(n,k):
T=[]
if k<0 or k>n: return T
TB=range(1,k+1); T.append(TB[:])
if k==0 or k==n: return T
i=k-1
while i>=0:
p=TB[i]+1
while i<k:
TB[i]=p; i=i+1; p=p+1
T.append(TB[:])
i=k-1
while i>=0 and TB[i]==i+n-k+1:
i=i-1
return T
def NbCommun(T1,T2):
nb=0; i=0; j=0;
while i<len(T1) and j<len(T2):
if T1[i]<T2[j]:
i=i+1
elif T1[i]>T2[j]:
j=j+1
else:
i=i+1; j=j+1; nb=nb+1
return nb
def Soluce(n,k):
T1=[]; T2=Cnk(n,k);
while len(T2)>0:
i=int(len(T2)*random.random())
TB=T2[i]; del T2[i]
i=0
while i<len(T1) and NbCommun(T1[i],TB)<=1:
i=i+1
if i==len(T1):
T1.append(TB)
return T1
def Test(n,k):
max=0
for i in range(1000):
T=Soluce(n,k)
if len(T)>max:
max=len(T)
print len(T),T
return max
Test(9,3)
Oui, c'est que j'ai tapé ça à la "vite fait" sous l'environnement que j'ai au boulot et que c'est l'ancienne version de python 2.7. Avec la version 3.4, ça donne ça :lapuce798 a écrit:Après avoir télécharger Python, la formule ne fonctionne pas "invalid synthax" alors que j'ai pris la formule telle quelle pour essayer d'obtenir les 12 sous ensemble.
Je crois comprendre que le problème vient de " print len(T),T "
Une idée ?
import random
def Cnk(n,k):
T=[]
if k<0 or k>n: return T
TB=list(range(1,k+1)); T.append(TB[:])
if k==0 or k==n: return T
i=k-1
while i>=0:
p=TB[i]+1
while i<k:
TB[i]=p; i=i+1; p=p+1
T.append(TB[:])
i=k-1
while i>=0 and TB[i]==i+n-k+1:
i=i-1
return T
def NbCommun(T1,T2):
nb=0; i=0; j=0;
while i<len(T1) and j<len(T2):
if T1[i]<T2[j]:
i=i+1
elif T1[i]>T2[j]:
j=j+1
else:
i=i+1; j=j+1; nb=nb+1
return nb
def Soluce(n,k):
T1=[]; T2=Cnk(n,k);
while len(T2)>0:
i=int(len(T2)*random.random())
TB=T2[i]; del T2[i]
i=0
while i<len(T1) and NbCommun(T1[i],TB)<=1:
i=i+1
if i==len(T1):
T1.append(TB)
return T1
def Test(n,k):
Max=0
for i in range(100):
T=Soluce(n,k)
if len(T)>Max:
Max=len(T)
print (len(T),T)
return Max
Test(9,3)
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 80 invités
Tu pars déja ?
Identification
Pas encore inscrit ?
Ou identifiez-vous :