Affichage des articles dont le libellé est Python. Afficher tous les articles
Affichage des articles dont le libellé est Python. Afficher tous les articles

lundi 31 juillet 2017

Algorithme de la fonction d'Ackerman en python


def ack(m,n):

    if m==0:
        return(n+1)


    elif n==0:


        return(ack(m-1,1))


    else:


        return(ack(m-1,ack(m,n-1)))

Algorithme python résolvant le problème des tours de Hanoï en python


Conditions initiales : 1 anneaux sur la tour "a", 2 sur la "b" et 3 sur la "c".

def hanoi(n,a=1,b=2,c=3):
   
    if n > 0:
       
        hanoi(n-1,a,c,b)
        hanoi(n-1,b,a,c)

Algorithme calculant de manière recursive x**n en python


def puissance(x,n): #calcul recursif de x**n
 
    if n==0:
        return 1
    else:
        return x*puissance(x,n-1)

Algorithme renvoyant l'écriture binaire d'un nombre sous la forme d'une chaîne de caractère en python



def base10to2(N,mot=''): # renvoie l'écriture binaire de N sous la forme d'une chaîne de caractère
   
    if N==0:
        return inverse(mot)
       
    else:
        r=N%2
        mot=mot+'%s'%r
       
       
        return base10to2(N//2,mot)

Algorithme testant si un mot est un palindrome en python


def inverse(mot):

    if len(mot)<=1:
        return mot
    else:
        return mot[-1]+inverse(mot[0:len(mot)-1])

 
def palindrome(mot):

    mot_inv=inverse(mot)
    if mot_inv==mot:
        return True
    else:
        return False

Algorithme renvoyant l'inverse d'un mot en python



def inverse(mot):

    if len(mot)<=1:
        return mot
    else:
        return mot[-1]+inverse(mot[0:len(mot)-1])

Algorithme renvoyant le pgcd de deux nombres en python


def pgcd(a,b):

    M=max(a,b)
    m=min(a,b)

    if m==0:
        return M
    else:
        M=m
        m=M%m
        return pgcd(m,M)

Algorithme de tri rapide en python


#Pour trier une liste on peut aussi utiliser L.sort()

def tri_rapide(L):
        if len(L)<=1:
                return L
        else:
                L1,L2=[],[]
        p=random.randint(0,len(L))
        for i in range(0,len(L)):
                if i!=p:
                        if L[i]<=L[p]:
                                L1.append(L[i])
                        else:
                                L2.append(L[i])
        return tri_rapide(L1)+[L[p]]+tri_rapide(L2)

Algorithme de tri fusion en python




 #Pour trier une liste on peut aussi utiliser L.sort()




def tri_fusion(L):
        if len(L)<=1:
                return(L)
        else:
                m=len(L)//2
        return fusion(tri_fusion(L[0:m]),tri_fusion(L[m:]))

 


def fusion(L_1,L_2):
        L=[]
        k,l=len(L_1),len(L_2)
        i,j=0,0
        while i<k and j<l:
                if L_1[i]<=L_2[j]:
                        L.append(L_1[i])
                        i+=1
                else:
                        L.append(L_2[j])
                        j+=1
        if i==k and j<l:
                L=L+L_2[j:]
        elif j==l and i<k:
                L=L+L_1[i:]
        return L

Algorithme de tri stupide en python


#Pour trier une liste on peut aussi utiliser L.sort()

import random as random

def tri_stupide(L):
       
        tri=True
        for i in range(len(L)-1):
                if L[i+1]<L[i]:
                        tri=False
                while tri==False:
                        i=random.randint(0,len(L)-1)
                        j=i=random.randint(0,len(L)-1)
                        L[i]=L[j]
                        print(L)

Algorithme de tri à bulles en python



#Pour trier une liste on peut aussi utiliser L.sort()

def tri_bulles(L):
        n=len(L)
        pasfini=True
        while pasfini:
                pasfini=False
                for i in range(n-1):
                        if L[i]>L[i+1]:
                                L[i],L[i+1]=L[i+1],L[i]
                                pasfini=True
                                print(L)
                n=n-1

Algorithme de tri par insertion en python


#Pour trier une liste on peut aussi utiliser L.sort()

def tri_insertion(L):
    n=len(L)
    for i in range(1,n):
        j=i
        x=L[i]
        while j>0 and L[j-1]>x:
            L[j]=L[j-1]
            j=j-1
           
        L[j]=x

Algorithme de tri par sélection en python


#Pour trier une liste on peut aussi utiliser L.sort()
 


 

def tri_selection(L):
    n=len(L)
    for i in range(n-1):
        i_min=i
        for j in range(i+1,n):
            if L[j] < L[i_min]:
                i_min=j
               
        if i!=i_min:
            L[i],L[i_min]=L[i_min],L[i]

samedi 17 juin 2017

Algorithme python combinaison k parmi n

import math as mp

def binom(n,k):
         return(mp.factorial(n)/(mp.factorial(n-k)*mp.factorial(k)))

Algorithme python renvoyant les racines d'un polynôme


 L : liste des coefficients du polynôme commençant par le coefficient dominant (lié au terme de plus haut degré)


 import numpy as np
def racines(L):
      p=np.poly1d(L)
      return(p.roots)

samedi 3 juin 2017

Aglorithme python retournant une liste sans doublons

Si la liste n'est pas préalablement triée par ordre croissant commencer l'algorithme par L.sort()

def ensemble(L):
    n=len(L)
    LR=[]
    for i in range(0,n-1):
         if L[i]!=L[i+1]:
             LR.append(L[i])
    LR.append(L[len(L)-1])
    return(LR)

Algorithme python renvoyant la liste des valeurs propres de la matrice A

import numpy.linalg as mp

def val_pr(a):
    L=mp.eig(A)[0]
    return(tri(L))

Algorithme python de tri d'une liste par ordre croissant

def tri(L):
    L1=[i for i in L]
    n=len(L1)
    for i in range(0,n-1):
        i_min=i
        for j in range(i+1,n):
            if L1[j]<L1[i_min]:
                i_min=j
        if i!=i_min:
            L1[i],L1[i_min]=L1[i_min],L1[i]
    return(L1)

Algorithme python affichant les valeurs propres de la matrice A

import numpy.linalg as mp
print(mp.eig(A))

Algorithme python renvoyant l'isobarycentre du triangle

 Soit M0, M1, M2 les points du plan complexe d'affixe respectves : z0=0, z1=1, z2= exp(i*pi/3)

def points(n):
    Abs=[0,1,1/2]
    Ord=[0,0,(m.sqrt(3))/2]
   
    for k in range(2,n+1):
        Abs.append((Abs[k-2]+Abs[k-1]+Abs[k])/3)
        Ord.append((Ord[k-2]+Ord[k-1]+Ord[k])/3)
    return(Abs,Ord)