diff --git a/Info/.DS_Store b/Info/.DS_Store new file mode 100644 index 0000000..d85a630 Binary files /dev/null and b/Info/.DS_Store differ diff --git a/Info/Td/TD11: Marc, Louise et Hugues.py b/Info/Td/TD11: Marc, Louise et Hugues.py new file mode 100644 index 0000000..84048d7 --- /dev/null +++ b/Info/Td/TD11: Marc, Louise et Hugues.py @@ -0,0 +1,130 @@ +import random + +"""Partie 1: Tri par sélection.""" + +def reperer(liste,n): + """Repere la position du plus grand element parmi les n premiers elements d'une liste.""" + maximum=liste[0] #Affecte a maximum la valeur du premier élément de la liste. + position=0 #Affecte a position la valeur de 0. + for i in range(1,n): #Fait varier les valeurs de i de 1 à n. + if liste[i]>maximum: #Verifie si la ie valeur de la liste est supérieur a la valeur de maximum. + maximum=liste[i] #Si oui, affecte a maximum la valeur du ie valeur de la liste. + position=i #Recupere la position de cet élément. + return position #Renvoie la position de cet élément. + +def echanger(liste,position1,position2): + """Echange 2 éléments d'une liste de positions données.""" + valeur=liste[position1] #Affecte a valeur la valeur de position1e terme de la liste. + liste[position1]=liste[position2] #Mets en position1 le position2e terme de la liste. + liste[position2]=valeur #Affecte au position2e terme de la liste la valeur de valeur. + return liste #Renvoie la nouvelle liste de termes échangés. + +def TriParSelection(liste): + """Tri une liste.""" + for n in range(len(liste)-1,0,-1): #Fait varier les valeurs de n de len(liste)-1 à 1. + r=reperer(liste,n) #Recupere la position du plus grand élément parmi les n premiers. + liste=echanger(liste,n,r) #Echange les éléments de position n et r. + return liste #Renvoie la liste triée. + +def test(a,b,taille,nombre=10): + """Test:""" + for i in range(nombre): #Fait nombre tests et affiche les résultats. + liste=[random.randint(a,b) for i in range(taille)] + print("Test:",i+1) + print(liste) + print(principale(liste)) + print("") + +#test(0,10,100) + +"""Partie 2: Tri par insertion + +10) +ALGORITHME: TRIPARINSERTION: + +DONNES: L : +LT <- [] +POUR i DE 0 JUSQU A LA LONGUEUR DE LA LONGUEUR DE L FAIRE + AJOUTER A LT LA VALEUR L[i] + k <- i + cle <- LT[-1] + TANT QUE cle0 FAIRE + LT[k] <- LT[k-1] + k <- k-1 + FIN TANT QUE + LT[k] <- cle +FIN POUR +AFFICHER LT + +11) +""" +def TriParInsertion(L): + """Tri une liste par insertion.""" + LT=[] + #Recupere les éléments de L et les ajoute dans LT de façon a ce que LT soit triée + for i in range(len(L)): + #Ajoute la valeur de L considérée et stocke la valeur de la clé + LT.append(L[i]) + k=i + cle=LT[-1] + #Décale la valeur de la clé dans LT tant que la clé est supérieur a ses éléments en partant de la fin + while cle0: + LT[k]=LT[k-1] #Réaffecte la valeur de l'élément k a la valeur de l'élément k-1. + k-=1 + #Une fois que l'on trouve que la clé est inférieure à un élément ou que l'on a considéré tous les éléments de LT + LT[k]=cle + #Renvoie la liste triée + return LT + +""" +13) Justesse du tri par sélection + +Algorithme: Reperer +Pas de boucle + +Algorithme: Echanger +Pas de boucle + +Algorithme: TriParSelection +Invariant de boucle: ... + +Algorithme: Test +Invariant de boucle: ... + + +14) Justesse du tri par insertion + +Algorithme: Tri par insertion +Invariant de boucle: ... + + +15) +""" + +def TriParInsertionEtComplexite(L): + complexite=0 + LT=[] + complexite+=1 + for i in range(len(L)): + LT.append(L[i]) + k=i + cle=LT[-1] + complexite+=4 + while cle0: + LT[k]=LT[k-1] + k-=1 + complexite+=4 + complexite+=1 + LT[k]=cle + complexite+=1 + complexite+=1 + return [LT,complexite] + + +#16) + +L=[random.randint(0,10) for e in range(100)] + +Trie,complexite=TriParInsertionEtComplexite(L) +print("Liste triee:",Trie) +print("Complexite:",complexite) diff --git a/Info/Td/__pycache__/converter.cpython-36.pyc b/Info/Td/__pycache__/converter.cpython-36.pyc new file mode 100644 index 0000000..057f256 Binary files /dev/null and b/Info/Td/__pycache__/converter.cpython-36.pyc differ diff --git a/Info/Td/converter.py b/Info/Td/converter.py new file mode 100644 index 0000000..b348d3e --- /dev/null +++ b/Info/Td/converter.py @@ -0,0 +1,260 @@ +import inspect + + +def test(arg1,arg2): + a = arg1 + arg2 + return a + + #do something with args + +keywords=["assert","break","class","continue","def","del","elif","else","except","finally","for", + "from","global","if","import","in","pass","print","raise","return","try","while","yield"] + +conversion1 = {"assert":"ASSERT", + "break":"SORTIR DE LA BOUCLE", + "class":"CLASSE", + "continue":"CONTINER", + "def":"DONNEES", + "del":"SUPPRIMER", + "elif":["SINON SI","FAIRE"], + "else":["SINON","FAIRE"], + "except":"EXCEPTE", + "finally":"FINALLEMENT", + "for":["POUR","FAIRE"], + "from":"DEPUIS", + "global":"GLOBAL", + "if":"SI", + "import":"IMPORTER", + "in":"DANS", + "pass":"PASSER", + "print":"AFFICHER", + "raise":"RELEVER", + "return":"AFFICHER", + "try":"ESSAYER", + "while":["TANT QUE","FAIRE"], + "yield":"PASSER AU TOUR SUIVANT", + "==":"=", + "=":"<-"} + + + +def algorithm(name): + lines = inspect.getsource(name) + print(lines) + output = [] + lines = lines.split("\n") + lines=removeUselessIndent(lines) + lines=Converter(lines) + for line in lines: + output.append(Converter(line)()) + #print("output: ",output) + return "\n".join(output) + + +class Converter: + def __init__(self,name): + self.name=name + self.code=inspect.getsource(name) + print(self.code) + self.lines=self.code.split("\n") + self.indent_historic=[] + self.indent=0 + self.cleanCode() + + self.conversion= {"assert": [[None,"assert",None], [None,"ASSERT",None]], + "break": [[None,"break"], [None,"SORTIR DE LA BOUCLE"]], + "class": [["class",None], ["CLASS",None]], + "continue": [[None,"continue",None], [None,"CONTINUER",None]], + "def": [["def",None,"(",None,"):"], [None,":\nDONNES: ",None," :"]], + "del": [[None,"del",None], [None,"SUPPRIMER",None]], + "elif": [[None,"elif",None,":"], [None,"SINON SI",None," FAIRE"]], + "else": [[None,"else:",None], [None,"SINON",None," FAIRE"]], + "except": [[None,"except:",None], [None,"EXCEPTE",None]], + "finally": [[None,"finally:",None], [None,"FINALLEMENT",None]], + "for": [["for",None," in ",None,":"], ["POUR",None," DANS ",None," FAIRE"]], + "from": [["from",None,"import",None], ["DEPUIS",None,"IMPORTER",None]], + "global": [["global",None], ["GLOBAL",None]], + "if": [[None,"if",None], [None,"SI",None]], + "import": [[None,"import",None], [None,"IMPORTER",None]], + " in ": [[None," in ",None], [None, "DANS ",None]], + " is ": [[None," is ",None], [None," EST ",None]], + "lambda": [[None,"lambda",None,":",None], [None,":\nDONNEES: ",None," :\nRENVOIE",None]], + "pass": [[None,"pass"], [None,"PASSER",None]], + "print": [[None,"print",None,"(",None,")"], [None,"AFFICHER: ",None,None]], + "raise": [[None,"raise",None], [None,"RELEVER",None]], + "return": [[None,"return",None], [None,"AFFICHER",None]], + "try": [[None,"try:",None], [None,"ESSAYER",None]], + "while": [["while ",None,":"], ["TANT QUE ",None," FAIRE"]], + "yield": [[None,"yield"], [None,"PASSER AU TOUR SUIVANT"]], + "True": [[None,"True",None], [None,"VRAI",None]], + "False": [[None,"False",None], [None,"FAUX",None]], + "==": [[None,"==",None], [None,"=",None]], + "!=": [[None,"!=",None], [None,"≠",None]], + "=": [[None,"=",None], [None," <- ",None]], + "append": [[None,".append(",None,")"], ["AJOUTER A ",None," LA VALEUR ",None]], + "not": [[None,"not",None], [None,"NON",None]]} + + + def cleanCode(self): + self.cleanIndents() + + + def cleanIndents(self): + self.line=self.lines[0] + count=self.countIndent(self.line) + for i in range(len(self.lines)): + self.lines[i]=self.lines[i][4*count:] + + def trackIndent(self,i): + print(i) + indent=self.countIndent(self.lines[i]) + if indent>len(self.indent_historic): + self.addElementIndentHistoric(i) + if indent1: B.derivate() derivatives.append(deepcopy(B)) + return derivatives + + def roots(self): + """Return the list of all the roots of the polynomial.""" + """Does that by finding where all the derivates cancel, to find new extrema and applying tvi.""" + roots=[] + derivatives=self.allDerivatives() print(derivatives) derivatives.reverse() - oldextrema=[] + old_extrema=[] for derivative in derivatives: - newextrema=[] - neglim=lim(derivative,-Infinity()) - poslim=lim(derivative,Infinity()) - if type(neglim)==Infinity: - neglim=~neglim - if type(poslim)==Infinity: - poslim=~poslim - extrema=[neglim]+oldextrema+[poslim] + new_extrema=[] + neglim=self.definition[0] + poslim=self.definition[1] + extrema=[neglim]+old_extrema+[poslim] for i in range(len(extrema)-1): - print("extrema:",extrema[i],extrema[i+1]) - print("derivatives:",derivative(extrema[i]),derivative(extrema[i+1])) - print("") if derivative(extrema[i])*derivative(extrema[i+1])<=0: - newextrema.append(derivative.gradientDescent(extrema[i],extrema[i+1])) - print("newextrema:",newextrema) - print("") - print("") - oldextrema=newextrema[:] - return newextrema + new_extrema.append(derivative.gradientDescent(extrema[i],extrema[i+1])) + old_extrema=new_extrema[:] + return new_extrema - def gradientDescent(self,a,b,precision=10e-10): - """Return a int value x between a and b for which the polynomial canceled itself.""" - print("descent",self(a),self(b)) - if (self(a)>0 and self(b)>0) or (self(a)<0 and self(b)<0): + def gradientDescent(self,xa,xb,precision=10e-10): + """Return a float value x between a and b for which the polynomial canceled itself.""" + """Does that based on theoreme of intermediate value with dichotomy.""" + if self(xa)*self(xb)>0: raise Exception("The polynomial cannot cancel itself within this interval.") - return None - if a>b: - c=a - a=b - b=c + a=min(xa,xb) + b=max(xa,xb) x=(a+b)/2 while abs(self(x))>precision: - print("descending:",x,self(x)) - if self(x)>0: + if self(x)*self(b)>0: b=x - if self(x)<0: + if self(x)*self(a)>0: a=x x=(a+b)/2 return x @@ -271,21 +254,21 @@ class Polynomial: """Return a string of the decomposition in simple elements of the polynomial.""" pass - def show(self,zone=[-5,5],precision=0.1): - """Show the polynomial using matplotlib.""" - Grapher([self])() - """ + def show(self): + """Show the polynomial with grapher.""" + Grapher([self])() #Instance Grapher only takes the list of the functions in parameter and can be called to display them. + + def matplotlibShow(self,zone=[-5,5],precision=0.1): + """Show the polynomial with matplotlib using optional zone and precision.""" z,Z=zone X=list([precision*x for x in range(int(z/precision),int(Z/precision))]) Y=list([self(x) for x in X]) plt.plot(X,Y,label="Polynomial") plt.show() - print("executed") - """ - - if __name__ == "__main__": + #suite_random = [random.randint(0,10) for i in range(10)] #Create polynome as big as needed. + #X=Polynomial(suite_random) suite1 = [3,2,4,5,1] suite2 = [1,4,1] suite3 = [4,5] @@ -302,6 +285,6 @@ if __name__ == "__main__": print("Unit polynomial of C =",C.unit()) print("HCF (PGCD en francais) of A and B (written A^B) =",A^B) print("LCM (PPCM en francais) of A and B (written A|B) =",A|B) - #print(A.gradientDescent(-10,10)) - print(A.roots()) + print("Roots of A:",A.roots()) + print("Show A on screen:") A.show() diff --git a/Teletype/matrices.py b/Teletype/matrices.py index caea707..07f0e35 100644 --- a/Teletype/matrices.py +++ b/Teletype/matrices.py @@ -8,7 +8,6 @@ from copy import deepcopy def pgcd(a,b): t = b b = a % b - if b == 0: return t else: @@ -71,7 +70,7 @@ def pgcdl(l): if __name__=="__main__": - c=5 #taille de la matrice carre de depart + c=6 #taille de la matrice carre de depart a=np.array([[random.randint(0,10) for i in range(c)] for j in range(c)]) print(a) b=triangular(a) diff --git a/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc b/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc index dbe1976..044955d 100644 Binary files a/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc and b/Teletype/mymaths/__pycache__/infinity.cpython-36.pyc differ diff --git a/Teletype/mymaths/infinity.py b/Teletype/mymaths/infinity.py index f6adeb4..0d6124d 100644 --- a/Teletype/mymaths/infinity.py +++ b/Teletype/mymaths/infinity.py @@ -42,7 +42,6 @@ class Infinity: def __mul__(self,other): if other==0: raise Exception("Undetermined") - return None else: if sign(other)!=sign(self): return Infinity(0) @@ -51,7 +50,6 @@ class Infinity: def __truediv__(self,other): if type(other)==Infinity: raise Exception("Undetermined") - return None else: if sign(other)!=sign(self): return Infinity(0)