From f089a322824a22ac046c06b6bd45d4073a9baa10 Mon Sep 17 00:00:00 2001 From: MarcPartensky Date: Sat, 9 Mar 2019 00:41:20 +0100 Subject: [PATCH] relocated teletype file --- .DS_Store | Bin 6148 -> 6148 bytes Teletype/.DS_Store | Bin 0 -> 6148 bytes Teletype/Minmax samy.py | 28 ++ Teletype/Polynome.py | 211 +++++++++++++ Teletype/Puissance4 Samy/Puissance4.py | 83 +++++ .../__pycache__/mycolors.cpython-36.pyc | Bin 0 -> 757 bytes .../__pycache__/mywindow.cpython-36.pyc | Bin 0 -> 10078 bytes Teletype/Puissance4 Samy/mycolors.py | 18 ++ Teletype/Puissance4 Samy/mywindow.py | 294 ++++++++++++++++++ Teletype/Teletype.py | 3 + Teletype/matrices.py | 80 +++++ 11 files changed, 717 insertions(+) create mode 100644 Teletype/.DS_Store create mode 100644 Teletype/Minmax samy.py create mode 100644 Teletype/Polynome.py create mode 100644 Teletype/Puissance4 Samy/Puissance4.py create mode 100644 Teletype/Puissance4 Samy/__pycache__/mycolors.cpython-36.pyc create mode 100644 Teletype/Puissance4 Samy/__pycache__/mywindow.cpython-36.pyc create mode 100644 Teletype/Puissance4 Samy/mycolors.py create mode 100644 Teletype/Puissance4 Samy/mywindow.py create mode 100644 Teletype/Teletype.py create mode 100644 Teletype/matrices.py diff --git a/.DS_Store b/.DS_Store index 7a279d7d6878a86290334982d38a85f0750ec174..7ba41513250f3efc97f1a8757a7f7a7b093142ee 100644 GIT binary patch delta 58 zcmZoMXfc@J&nUPtU^g?P;AS3{VkTJ*h7g8Sh8%`eh7yKKh60Avvf!e;ocz3W1_lPk OjfL5ao7p-3@&f?Ckq?Rh delta 28 kcmZoMXfc@J&nU1lU^g?Pz-AtnVy2C`9gLgVIsWnk0Dy)F)&Kwi diff --git a/Teletype/.DS_Store b/Teletype/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmZQzU|@7AO)+F(5MW?n;9!8z45|!R0Z1N%F(jFgL>QrFAPJ2!M?+vV1V%$(Gz3ON zU^D~25V%SxcdJP zRior+2#kinunYl47MEZbCs3t{!+W4QHvuXKVuPw;Mo^s$(F3lEVT}ML$bg~*R5_@+ b2Uo?6kTwK}57Iu`5P${HC_Nei0}uiLNUI8I literal 0 HcmV?d00001 diff --git a/Teletype/Minmax samy.py b/Teletype/Minmax samy.py new file mode 100644 index 0000000..1aee719 --- /dev/null +++ b/Teletype/Minmax samy.py @@ -0,0 +1,28 @@ +""" +f : mvts possibles -> prochain meilleur mvt + +""" + + + + +l=[[1,2],[3,4]] +"""[1,2]=>2 +[3,4]=>4 +[2,4] +2""" + + +def minmax(arbre, level = 0, maxi = True): + if type(arbre[0]) is not list: + if max and level%2==0: + return max(arbre) + else: + return min(arbre) + else: + if max and level%2==0: + return max([minmax(e,level+1,maxi) for e in arbre]) + else: + return min([minmax(e,level+1,maxi) for e in arbre]) + +print(minmax(l)) diff --git a/Teletype/Polynome.py b/Teletype/Polynome.py new file mode 100644 index 0000000..22ba4e9 --- /dev/null +++ b/Teletype/Polynome.py @@ -0,0 +1,211 @@ +""" +Features: +1:COMPLETED: print #renvoie str +2:COMPLETED: addition #renvoie Polynomial +3:COMPLETED: multiplication #renvoie Polynomial +4:COMPLETED: derive #renvoie un Polynomial +5:COMPLETED: division #renvoie couple de Polynomial +6:COMPLETED: pgcd #renvoie Polynomial +7: ppcm #renvoie Polynomial +8: taylor #renvoie str ou liste "je sais pas encore" +9: factoriser #renvoie str ou liste de liste des coefficients "pareil" +10: racines reelles #renvoie liste des racines reelles +11: racines complexes #renvoie liste des racines complexes +12: show #Affiche le polynome sur une fenetre pygame, pyglet ou matplotlib +""" + +from copy import deepcopy + + +class Polynomial: + def __init__(self, coefficients): + """Create a polynomial using its coefficients.""" + self.coefficients = coefficients + def __repr__(self): + """Return the usual representation of a polynomial.""" + if self.coefficients==[]: + return "0" + liste = [] + if self.coefficients[0]!=0: + liste.append(str(self.coefficients[0])) + for degre,coefficient in enumerate(self.coefficients[1:],1): + if coefficient!=0: + liste.append(str(coefficient) + "X^" + str(degre)) + liste.reverse() + text = "+".join(liste) + text = text.replace("^1","").replace("+-","-").replace("1X","X") + return text + + def adapt(self,coefficients,size): + """Add zeros to the coefficients using coefficients and size.""" + coefficients += [0]*(size-len(coefficients)) + return coefficients + + def __add__(self, other): + """Add 2 polynomials together by adding their coefficients.""" + maxdegree = max(self.degree(),other.degree()) + cA = self.adapt(self.coefficients,maxdegree) + cB = self.adapt(other.coefficients,maxdegree) + newcoefficients = [a+b for (a,b) in zip(cA, cB)] + return Polynomial(newcoefficients) + + def __sub__(self, other): + """Substract 2 polynomials together by multiplying the second polynomial by -1 and adding it to the first using add method.""" + return self+other*Polynomial([-1]) + + def __mul__(self, other): + """Multiply 2 polynomials together using the formula for the coefficients.""" + degree = len(self.coefficients) + len(other.coefficients) + A = self.coefficients + [0]*(degree-len(self.coefficients)+1) + B = other.coefficients + [0]*(degree-len(other.coefficients)+1) + newcoefficients = [] + for k in range(degree + 1): + coeff = 0 + for i in range(k + 1): + coeff += A[i] * B[k - i] + newcoefficients.append(coeff) + C = Polynomial(newcoefficients) + C.correct() + return C + + def correct(self): + """Correct the coefficients of the polynomial by deleting useless zeros at the end,""" + while len(self.coefficients)>1: + if self.coefficients[-1] == 0: + del self.coefficients[-1] + else: + break + if self.coefficients==[0]: + self.coefficients=[] + """and then replace float coefficients by int coefficients if possible.""" + for i in range(len(self.coefficients)): + if self.coefficients[i]==int(self.coefficients[i]): + self.coefficients[i]=int(self.coefficients[i]) + + def __len__(self): + """Return the number of coefficients. + It is important to notice that it is not the degree due to the fact that + the polynomial might have a degree of -inf and the len method must have positive values. + In such a case this method will raise an error. + It is why it is better to use the degree method in the general case.""" + A=deepcopy(self) + A.correct() + return len(A.coefficients)-1 + + def degree(self): + """Return the degree of a polynomial. + It will return -1 if the degree is -inf to be computer friendly.""" + A=deepcopy(self) + A.correct() + return len(A.coefficients)-1 + + def __getitem__(self,index): + """Return a coefficient of the polynomial.""" + return self.coefficients[index] + + def __setitem__(self,index,value): + """Change a coefficient of the polynomial.""" + self.coefficients[index] = value + + def __delitem__(self,index): + """Delete a coefficient of the polynomial.""" + del self.coefficients[index] + + def derivate(self): + """Derivate the polynomial.""" + self.coefficients = [i*c for (i,c) in enumerate(self.coefficients)] + del self.coefficients[0] + + def derivative(self): + """Return the derivative polynomial.""" + newcoefficients = [i*c for (i,c) in enumerate(self.coefficients)] + del newcoefficients[0] + return Polynomial(newcoefficients) + + def unit(self): + """Return the polynomial unit form.""" + k=self.coefficients[-1] + newcoefficients=[] + for i in range(len(self.coefficients)): + newcoefficients.append(self.coefficients[i]/k) + A=Polynomial(newcoefficients) + A.correct() + return A + + def unitarize(self): + """Convert the polynomial into its unit form.""" + self=self.unit() + + def __xor__(self,other): + """Return the HCF (PGCD en francais) of self and other by Euclide algorithm. Make use of "^" operator.""" + R0 = deepcopy(self) + R1 = deepcopy(other) + R0.correct() + R1.correct() + listR=[R0,R1] + R2,Q2=R0/R1 + listR.append(R2) + while listR[-1].degree()>0: + R,Q=listR[-2]/listR[-1] + listR.append(R) + return listR[-2] + + #Resultat d'une enorme galere de 2h... + #Algorithme d'Euclide: + #a=b*q0+r0 + #b=r*q1+r1 + #r=r1*q2+r2 + #r1=r2*q3+r3 + #... + #ri=r(i-1)*q(i-1)+1+0 + + def __or__(self,other): + """Return the LCM (PPCM en francais) using "i have absolutely no clue how to do this so figure it out!" Make use of "|" operator.""" + #On sent la (tres) grosse fatigue apres minuit. + return "Figure it out!" + + def __truediv__(self, other): + """Return the division of 2 polynomials.""" + R = deepcopy(self) + B = deepcopy(other) + R.correct() + B.correct() + Q = Polynomial([]) + while R.degree()>=B.degree(): + d = R.degree()-B.degree()+1 + C = Polynomial([0 for i in range(d)]) + C[-1] = R[-1]/B[-1] + R = R-B*C + Q = Q+C + Q.correct() + R.correct() + return [R,Q] + + #Ce que j'ai du faire pour trouver l'implementation... + #On a: A=BQ+R + #[3,2,4,5,1]/[1,4,1] + #[3,2,3,1,0]=[3,2,4,5,1]-[0,0,1]*[1,4,1] #[0,0,1,4,1] + #[3,1,-1,0]=[3,2,3,1]-[0,1]*[1,4,1] #[0,1,4,1] + #[4,5,0]=[3,1,-1]-[-1]*[1,4,1] #[-1,-4,-1] + #[4,5] + + #5=8*0+5 cas particulier de la division euclidienne + + +if __name__ == "__main__": + suite1 = [3,2,4,5,1] + suite2 = [1,4,1] + suite3 = [4,5] + A = Polynomial(suite1) + B = Polynomial(suite2) + C = Polynomial(suite3) + print("A =",A) + print("B =",B) + print("C =",C) + print("A+B =",A+B) + print("A*B =",A*B) + print("Derivative of A =",A.derivative()) + print("A/B :",A/B) + print("Unit polynomial of C =",A.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) diff --git a/Teletype/Puissance4 Samy/Puissance4.py b/Teletype/Puissance4 Samy/Puissance4.py new file mode 100644 index 0000000..25e07c7 --- /dev/null +++ b/Teletype/Puissance4 Samy/Puissance4.py @@ -0,0 +1,83 @@ +import random + +class Puissance4: + def __init__(self): + self.board = Board() + self.players = [Player(0),Player(1)] + self.state = 0 + + def __call__(self): + while self.board.on: + self.board.check() + self.state += 1 + turn = self.state % 2 + player = self.players[turn] + player(self.board) + +class Board: + def __init__(self,size=[7,6]): + self.size = size + self.grid = [[-1 for y in range(size[1])] for x in range(size[0])] + + def check(self): + pass + + def randomfill(self): + sx,sy=self.size + for x in range(sx): + for y in range(sy): + self.grid[x][y]=random.randint(-1,1) + + def __repr__(self): + text="" + lx=[] + for x in range(self.size[0]): + ly=[] + for y in range(self.size[1]): + if self.grid[x][y]==-1: + ly.append(" ") + if self.grid[x][y]==0: + ly.append("X") + if self.grid[x][y]==1: + ly.append("O") + lx.append("|".join(ly)) + text="\n".join(lx) + return text + +class Player: + def __init__(self,side): + self.side = side + + def __call__(self,board): #Jouer + self.randomPlay(board) + + def randomPlay(self,board): + a=list(range(self.size[0])) + random.shuffle(a) + for e in a: + choice=self.choose(board,e) + if choice is not None: + x,y=choice + board.grid[x][y]=self.side + + def choose(self,board,x): + l=list(range(self.size[1])) + l.reverse() + for y in l: + v=self.grid[x][y] + if v==0 or v==1: + return (x,y) + return None + + + def play(self,board,x): + i = board.size[2] + if board.grid[x].count(-1) == 0 or x > "grosse valeur inutile juset la pour que le programme plante pas quand je le test": + while board.grid[x][i] != -1: + i -= 1 + +game = Puissance4() +game() + +#game.board.randomfill() +#print(str(game.board)) diff --git a/Teletype/Puissance4 Samy/__pycache__/mycolors.cpython-36.pyc b/Teletype/Puissance4 Samy/__pycache__/mycolors.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..46185a506dba85d80af5b1211c49e216e8848f3c GIT binary patch literal 757 zcmXr!<>g9u4Tw3z$iVQJ0SPcOFfceUFfbJRFfcHr@JBJE2t+Za2u3lb2t_fc2uHD` zh(xibh(@uch()odh(~dxNJMd_NJep`NJVj{NJsIc$VBm`$VTy{$VKs|3Z(EhGcq!y z@C7qy%D)6z^YT9f1H;P{5Mcx&L_h==hAmRjwI1VD#gNQB$28JrZ+{)zq zocy9 zE&>G%f`AEt^?{@`nTuFJ?tl}T%tfpq0XU(_T*L+vfD=Rsu!FR4fCx^A=Rjdq1Sd3^ zi?~6OJRpJ>q(U%?#mOhsHHtaN)g_A6J;>G7FN!VF)yKy_{1&T|kE65qE!J=k&k)xr z7WW|6$SAge(4YVx*C-ATM;|w^2#1SfkT;mc4q}1K;RG=tW`SkFHgNfPx_g8;1^I{j zMR9`JU`?z}uAc6$w^$2`GV@ASG874doCYF(`RIohrxq3K=jUXWWu_JtBo>vV<`rjG z>IW3%rxzvW7V8I8mSp7T>4&7|q?S|`r0NHhW)>GG<|U_^Cd-FTNZOxg@NHQ0}^0nU|?`yU|=Y2U|?WKVMt-jVaR2SVq^rdnR1vyKs0j> za};w5Lkd$43zW|q#hSv9!kohv#pceC!ji(;!jQt+%oN3*%8|mB%~VvA%AUgB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hRQc}1Yr zH$|Z7!6MMqSrMqeToeqF1$FR>VnM7p5CLv}fSPJW86YmGt}H4AvC2UNC}4^zK`e08 zAqY^00k3TVrQTxDdM^$}7A7eH4qgs1E;bGp4o(hk4lWKB4mJ)B4i+e6<6z<9KW=8 Ogg6*ESV1D7hB5$9SlMF$ literal 0 HcmV?d00001 diff --git a/Teletype/Puissance4 Samy/mycolors.py b/Teletype/Puissance4 Samy/mycolors.py new file mode 100644 index 0000000..9402063 --- /dev/null +++ b/Teletype/Puissance4 Samy/mycolors.py @@ -0,0 +1,18 @@ +BLUE = ( 0, 0,255) +RED = (255, 0, 0) +GREEN = ( 0,255, 0) +YELLOW = (255,255, 0) +BLACK = ( 0, 0, 0) +WHITE = (255,255,255) +GREY = (100,100,100) +PURPLE = (100, 0,100) +HALFGREY = ( 50, 50, 50) +DARKGREY = ( 20, 20, 20) +DARKRED = ( 10, 10, 10) +DARKGREEN = ( 10, 10, 10) +DARKBLUE = ( 10, 10, 10) +LIGHTBROWN = (229,219,222) +LIGHTGREY = (200,200,200) +BEIGE = (199,175,138) + +print("mycolors imported") diff --git a/Teletype/Puissance4 Samy/mywindow.py b/Teletype/Puissance4 Samy/mywindow.py new file mode 100644 index 0000000..8bdbdb2 --- /dev/null +++ b/Teletype/Puissance4 Samy/mywindow.py @@ -0,0 +1,294 @@ +from __future__ import division + +from mycolors import * + +import pygame +from pygame.locals import * +import time +import json + + + +class Window: + made=0 + + def __init__(self,name="Unnamed",size=None,text_font="monospace",text_size=65,text_color=WHITE,background_color=BLACK,fullscreen=False,build=True): + """Create a window object using name, size text_font, text_size, text_color, background and set.""" + Window.made+=1 + self.number=Window.made + self.name=name + self.size=size + self.text_font=text_font + self.text_size=text_size + self.text_color=text_color + self.background_color=background_color + self.fullscreen=fullscreen + self.set() + self.log("Window has been created.") + if build: + self.build() + + def set(self): + """Set builtins attributs of window object.""" + self.RIGHT = 0 + self.UP = 1 + self.LEFT = 2 + self.DOWN = 3 + self.selecter_color=self.reverseColor(self.background_color) + self.open=False + self.screenshots_taken=0 + self.pause_cool_down=1 + self.time=time.time() + self.pause_time=0.2 + + def build(self): + """Creates apparent window.""" + pygame.init() + self.info = pygame.display.Info() + self.font = pygame.font.SysFont(self.text_font, self.text_size) + if self.size is None: + if self.fullscreen: + self.size=[self.info.current_w,self.info.current_h] + else: + self.size=[2*self.info.current_w//3,2*self.info.current_h//3] + self.coordonnates=[0,0]+self.size + if self.fullscreen: + self.screen=pygame.display.set_mode(self.size,FULLSCREEN) + else: + self.screen=pygame.display.set_mode(self.size,RESIZABLE) + pygame.display.set_caption(self.name) + if self.text_color is None: + self.text_color=self.reverseColor(self.background_color) + self.clear() + self.flip() + self.open=True + + def clear(self,color=None): + """Clear to background color.""" + if color is None: + color=self.background_color + self.screen.fill(color) + + def scale(self,picture,size): + """Return scaled picture using picture and size.""" + return pygame.transform.scale(picture,size) + + def check(self): + """Update window's state depending if close buttons are pressed.""" + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.open=False + if event.type == KEYDOWN: + if event.key == K_ESCAPE: + self.open=False + + def update(self): + """Updates all window's main attributs.""" + self.mouse_click=bool(pygame.mouse.get_pressed()[0]) + self.mouse_position=pygame.mouse.get_pos() + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.open=False + if event.type == KEYDOWN: + if event.key == K_ESCAPE: + self.open=False + + def pause(self): + """Wait for user to click on space.""" + while self.open: + self.check() + keys=pygame.key.get_pressed() + if keys[K_SPACE]: + break + if self.open: + time.sleep(self.pause_time) + + def press(self): + """Return all keys.""" + return pygame.key.get_pressed() + + def direction(self): + """Return keys for arrows pressed. Trigonometric orientation is used.""" + keys=pygame.key.get_pressed() + return (keys[K_RIGHT],keys[K_UP],keys[K_LEFT],keys[K_DOWN]) + + + def select(self): + """Wait for user to click on screen, then return cursor position.""" + while self.open: + self.check() + for event in pygame.event.get(): + if event.type == MOUSEBUTTONDOWN and event.button == 1: + return (event.pos[0],event.pos[1]) + + def point(self): + """Return cursor position on screen.""" + return pygame.mouse.get_pos() + + def click(self): + """Return bool value for clicking on screen.""" + return bool(pygame.mouse.get_pressed()[0]) + + def press(self): + """Return bool value for clicking on screen.""" + return pygame.key.get_pressed() + + def flip(self): + """Refresh screen.""" + pygame.display.flip() + + def screenshot(self): + """Save picture of the surface.""" + self.picture_saved+=1 + pygame.image.save(self.screen,self.name+"-"+str(self.picture_saved)+".png") + + + def getPicture(self,picture_directory): + """Return picture using picture directory.""" + return pygame.image.load(picture_directory) + + def placePicture(self,picture_directory,coordonnates,color=None): + """Draw a picture on screen using pygame picture directory and position.""" + x,y,sx,sy=coordonnates + picture=pygame.image.load(picture_directory) + picture=pygame.transform.scale(picture,(sx,sy)) + if color is not None: + picture=colorize(picture,color) + self.screen.blit(picture, position) + + def centerText(self,message,size=None): + """Centers Text on screen.""" + sx,sy=self.size + if size is None: + size=self.text_size + l=len(message) + letter_size=size/4 + x=sx//2-letter_size*l//2 + y=sy//2-size/3 + return (x,y) + + def alert(self,message): + """Quickly display text on window.""" + position=self.centerText(message) + self.print(message,position) + self.flip() + + def print(self,text,position,size=None,color=None,font=None): + """Display text on screen using position, size, color and font.""" + if size is None: + size=self.text_size + if color is None: + color=self.text_color + if font is None: + font=self.font + label = font.render(text, 1, color) + self.screen.blit(label, position) + + def drawRect(self,coordonnates,color): + """Draw a rectangle on the screen using color and coordonnates relative to window's fiducials.""" + wsx,wsy=self.size + wcx,wcy,wcsx,wcsy=self.coordonnates + rcx,rcy,rcsx,rcsy=coordonnates + x,y=(rcx-wcx,rcy-wcy) + w,h=(rcsx*wsx/wcsx,rcsy*wsy/wcsy) + pygame.draw.rect(self.screen,color,(x,y,w,h),0) + + def place(self,position): + """Return position relative to window's fiducials.""" + wcx,wcy,wcsx,wcsy=self.coordonnates + pcx,pcy=position + x,y=(rcx-wcx,rcy-wcy) + return (x,y) + + + def randomColor(self): + """Return random color.""" + import random + r=random.randint(0,255) + g=random.randint(0,255) + b=random.randint(0,255) + color=(r,g,b) + return color + + def reverseColor(self,color): + """Return reverse color.""" + r,g,b=color + r=255-r + g=255-g + b=255-b + color=(r,g,b) + return color + + def lighten(self,color,luminosity=80): #View later + """Return lightened color using color and luminosity percentage.""" + r,g,b=color + if luminosity>=50: + r+=(255-r)*luminosity/100 + g+=(255-g)*luminosity/100 + b+=(255-b)*luminosity/100 + else: + r-=r*luminosity/100 + g-=g*luminosity/100 + b-=b*luminosity/100 + color=r,g,b + return color + + def colorize(self,image, color): + """Return image colorized""" + image = image.copy() + image.fill((0,0,0,255),None,pygame.BLEND_RGBA_MULT) + image.fill(color[0:3]+(0,),None,pygame.BLEND_RGBA_ADD) + return image + + + + def wavelengthToRGB(self,wavelength): + """Convert wavelength to rgb color type.""" + gamma,max_intensity=0.80,255 + def adjust(color, factor): + if color==0: return 0 + else: return round(max_intensity*pow(color*factor,gamma)) + if 380<=wavelength<=440: r,g,b=-(wavelength-440)/(440-380),0,1 + elif 440<=wavelength<=490: r,g,b=0,(wavelength-440)/(490-440),1 + elif 490<=wavelength<=510: r,g,b=0,1,-(wavelength-510)/(510-490) + elif 510<=wavelength<=580: r,g,b=(wavelength-510)/(580-510),1,0 + elif 580<=wavelength<=645: r,g,b=1,-(wavelength-645)/(645-580),0 + elif 645<=wavelength<=780: r,g,b=1,0,0 + else: r,g,b=0,0,0 + if 380<=wavelength<=420: factor=0.3+0.7*(wavelength-380)/(420-380) + elif 420<=wavelength<=701: factor=1 + elif 701<=wavelength<=780: factor=0.3+0.7*(780-wavelength)/(780-700) + else: factor=0 + r,g,b=adjust(r,factor),adjust(g,factor),adjust(b,factor) + return (r,g,b) + + def kill(self): + """Quit pygame.""" + pygame.quit() + + def log(self,message): + """Print message with window mention.""" + text="["+self.name+"] "+message + print(text) + + def __del__(self): + """Executed before the window is destroyed.""" + self.log("Window has been closed.") + + + + + + + +if __name__=="__main__": + w=Window("Window Prototype") + #save(w,"grosse fenetre") + #w=load("grosse fenetre") + #print(w.lighten(BLUE)) + #w.alert("test") + w.pause() + w.clear() + w.alert("je raconte de la merde juste pour avoir une longue chaine de caractere") + w.pause() + w.kill() diff --git a/Teletype/Teletype.py b/Teletype/Teletype.py new file mode 100644 index 0000000..03b2a11 --- /dev/null +++ b/Teletype/Teletype.py @@ -0,0 +1,3 @@ +import pygame + +from mywindow import Window diff --git a/Teletype/matrices.py b/Teletype/matrices.py new file mode 100644 index 0000000..caea707 --- /dev/null +++ b/Teletype/matrices.py @@ -0,0 +1,80 @@ +import numpy as np +from math import sqrt +import random +from copy import deepcopy + + + +def pgcd(a,b): + t = b + b = a % b + + if b == 0: + return t + else: + return pgcd(t,b) + +def ppcm(a, b): + return a * b // pgcd(a, b) + +def transvection(a,i,j): + """Anciennement appelee la sainte transvection""" + if a[j][i]!=0: + p = ppcm(a[i][i],a[j][i]) + k1 = p//a[i][i] + k2 = p//a[j][i] + a[i] *= k1 + a[j] *= k2 + a[j] = a[i]-a[j] + return a + +def transpose(a): + """Echange l'abscisse et l'ordonnee pour chaque case""" + b=np.array([[a[i][j] for i in range(a.shape[1])] for j in range(a.shape[0])]) + return b + +def exchange(a,j1,j2): + """Li1 <-> Li2""" + l = deepcopy(a[j1]) + a[j1] = deepcopy(a[j2]) + a[j2] = l + return a + +def index_pivot(a, i,jmin): + """Renvoie un pivot""" + for j in range(jmin,len(a)): + if a[j][i] != 0: + return j + +def triangular(a): + """Renvoie la matrice triangularise""" + for i in range(len(a[0])-1): + jmin=i+1 + jp = index_pivot(a,i,jmin) + if a[i][i]==0: + a = exchange(a,i,jp) + for j in range(jmin,len(a)): + a = transvection(a,i,j) + for j in range(len(a)): + a[j]=a[j]/pgcdl(a[j]) + return a + +def identity(a): + pass + +def pgcdl(l): + """Renvoie le pgcd d'une liste""" + p=l[0] + for i in range(1,len(l)): + p=pgcd(p,l[i]) + return p + + +if __name__=="__main__": + c=5 #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) + print(b) + #c=transpose(b) + #print(c)