• # Essai

    Posté par . En réponse au message Besoin d'avis sur algo Python. Évalué à 4.

    Je suis loin d'être un pro niveau optimisation, je suis un peu dans le même cas que toi, j'ai juste essayé de diminuer le nombre de lignes. Aucune idée si ça a une influence sur la rapidité d'exécution.

    #!/usr/bin/env python3
    # rot13.py - ROT13 encoder/decoder written in Python.
    from sys import argv
    from os.path import basename
    def rot13(txt):
     """Encoder / decoder la chaine txt."""
     A2Z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     MAX = len(A2Z)
     MOY = MAX // 2 - 1
     res = "" # Pas besoin d'initialiser tmp ou pos tu es en python !
     for car in txt:
     tmp = car.capitalize() # Pas besoin de tester si c'est une capitale
     if A2Z.count(tmp):
     pos = A2Z.index(tmp) - MOY # Tu pourrais te passer de la variable pos
     tmp = A2Z[pos] # En python si tu appelles un index negatif dans une liste tu obtiens un element en partant de la fin de la liste
     if car.islower():
     tmp = tmp.lower()
     res += tmp
     return(res)
    if len(argv) > 1:
     for arg in argv[1:len(argv)]:
     print(rot13(arg))
    else:
     print("Usage: {0} <chaine1> <chaine2>".format(basename(argv[0])))
    
    

    sinon une version plus bourrin en une ligne (list comprehension), je peux détailler si tu le souhaites :

    #!/usr/bin/env python3
    # rot13.py - ROT13 encoder/decoder written in Python.
    from sys import argv
    from os.path import basename
    def rot13(txt):
     """Encoder / decoder la chaine txt."""
     A2Z = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
     MOY = len(A2Z) // 2
     res = ''.join([ A2Z[A2Z.index(car) - MOY] if A2Z.count(car) else car for car in txt ])
     return(res)
    if len(argv) > 1:
     for arg in argv[1:len(argv)]:
     print(rot13(arg))
    else:
     print("Usage: {0} <chaine1> <chaine2>".format(basename(argv[0])))