il dérive tel un radeau
from math import * #---------------------- #Polynome #---------------------- def r_coefs(expression,debug=False): coefs = [] termes = [] car = ["0","1","2","3","4","5","6","7","8","9","-","+","(",")","*","^","/","x","X",""," "] etat,caractere=check(expression,car) if etat == False: print("caractère invalide :",caractere) print("Les caractères autorisés sont :",car) return None poly = nettoyage(expression) if debug : print("---Polynome---") print("") print("Le polynome est :") print(poly) #recherche du plus grand exposant exp_max = 0 for i,elt in enumerate(poly): if elt == "x" and poly[i+1] != "+" and poly[i+1] != "-": if exp_max < int(poly[i+1]): exp_max = int(poly[i+1]) if debug : print("Polynome de degrès",exp_max) exposants = list(range(exp_max+1)) del exposants[0] exposants.reverse() #liste des exposants pour ordonner la liste des coefs #recherche des coeeficients for i,elt in enumerate(poly): if elt =="-" or elt =="+": nb = elt for j in poly[i+1:]: if j == "+" or j=="-": break #j'aime pas les breaks mais la flm else : nb += j termes.append(nb) #Mis en ordre des coefs for i in exposants: n = "x{}".format(i) if n in poly: for j in termes: if n in j : coefs.append(j[:-2]) else: coefs.append(0) if debug : print("les termes de ce polynome sont :", termes) print("Et ses coefficients (utiles) sont :",coefs) return coefs #---------------------- #Utilitaires #---------------------- def nettoyage(arg): ''' :param arg: Chaine à "nétoyer" (normaliser) :return: revoie la chaines libile par l'analyseur de syntaxe ''' arg = arg.replace(" ","") arg = arg.replace("^","") arg = arg.replace("*","") arg = arg.replace("**","") arg = arg.lower() if arg[0] != '-': arg ='+' + arg arg="".join(arg) sortie="" #débug temporaire if arg[-1] =="x": arg += "+0" #ajout du 1x for i,j in enumerate(arg): if (j == "x" and ( arg[i-1] == "+" or arg[i-1] == "-")): sortie += "1"+j+"1" elif j=="x" and ( arg[i+1] == "+" or arg[i+1] == "-"): sortie += j+"1" else : sortie += j sortie = sortie.replace(" ","") return sortie def check(expression,liste): ''' :param expression: Expression à vérfier :param liste: Liste des caractères valides :return: Vrai si l'expression est bonne et Faux si un caractère est invalide ''' for i in expression: if i not in liste: return (False,i) return True,None print(r_coefs("10x5-4x"))