[python] comment simplifier une fonction

Résolu/Fermé
AwwA - 3 mai 2008 à 17:37
 AwwA - 4 mai 2008 à 11:59
Bonjour,

je viens de creer une fonction qui marche bien mon seul soucis c'est que elle est bien longue et qu'il m'en reste
d'autre plus complexe encore


def paire(main):
    

    listeP=main
    #0
    a=list(listeP[0])
    #1
    b=list(listeP[1])
    #2
    c=list(listeP[2])
    #3
    d=list(listeP[3])
    #4
    e=list(listeP[4])
    #5
    f=list(listeP[5])
    #6
    g=list(listeP[6])
    wow=[]
    if a[0:2]==b[0:2]:
        wow=main[0],main[1]
        return 1,wow
    if a[0:2]==c[0:2]:
        wow=main[0],main[2]
        return 1,wow
    if a[0:2]==d[0:2]:
        wow=main[0],main[3]
        return 1,wow
    if a[0:2]==e[0:2]:
        wow=main[0],main[4]
        return 1,wow
    if a[0:2]==f[0:2]:
        wow=main[0],main[5]
        return 1,wow
    if a[0:2]==g[0:2]:
        wow=main[0],main[6]
        return 1,wow
    if b[0:2]==c[0:2]:
        wow=main[1],main[2]
        return 1,wow
    if b[0:2]==d[0:2]:
        wow=main[1],main[3]
        return 1,wow
    if b[0:2]==e[0:2]:
        wow=main[1],main[4]
        return 1,wow
    if b[0:2]==f[0:2]:
        wow=main[1],main[5]
        return 1,wow
    if b[0:2]==g[0:2]:
        wow=main[1],main[6]
        return 1,wow
    if c[0:2]==d[0:2]:
        wow=main[2],main[3]
        return 1,wow
    if c[0:2]==e[0:2]:
        wow=main[2],main[4]
        return 1,wow
    if c[0:2]==f[0:2]:
        wow=main[1],main[5]
        return 1,wow
    if c[0:2]==g[0:2]:
        wow=main[1],main[6]
        return 1,wow
    if d[0:2]==e[0:2]:
        wow=main[3],main[4]
        return 1,wow
    if d[0:2]==f[0:2]:
        wow=main[3],main[5]
        return 1,wow
    if d[0:2]==g[0:2]:
        wow=main[3],main[6]
        return 1,wow
    if e[0:2]==f[0:2]:
        wow=main[4],main[5]
        return 1,wow
    if e[0:2]==g[0:2]:
        wow=main[4],main[6]
        return 1,wow
    if f[0:2]==g[0:2]:
        wow=main[5],main[6]
        return 1,wow


elle prend ce type de liste ['07h', '14c', '07c', '14d', '02c', '04s', '09d']

et retourne 1 si elle voit une paire et la paire en question

merci beaucoup

1 réponse

jisisv Messages postés 3645 Date d'inscription dimanche 18 mars 2001 Statut Modérateur Dernière intervention 15 janvier 2017 934
4 mai 2008 à 10:04
Ceci est plus compact, plus lisible moins "error prone":
#!/usr/bin/python
def getPairs(aHand):
    pairs=[]
    for c1 in range(0,7):
        for c2 in range(c1 + 1, 6 -c1):
            if aHand[c1][0:2] == aHand[c2][0:2]:
                pairs.append((c1,c2))
    return pairs

myHand = ['07h', '14c', '07c', '14d', '02c', '04s', '09d']
result = getPairs(myHand)
for aPair in result :
    print "A pair found %s %s" % (myHand[aPair[0]] , myHand[aPair[1]])


Tu peux rendre ce code plus objet, changer la taille de la main pour les martiens et les jolies vénusiennes jouant au poker etc... Bon amusement.

Johan
1
merci beaucoup ,je pensais pas que on pouvait la rendre aussi courte !
0