Listes

Listes

## Exercice 1

def longueur(l):
    long = 0
    for x in l:
        long += 1
    return long

## Exercice 2

def est_dans(x,l):
    b = False
    for y in l:
        b = x==y
    return b

def est_dans_indice(x,l):
    b = False
    i = 0 #Regarde ou on en est du parcours de la liste
    indice = 0 #sauvegarde l'indice de x si on le trouve
    for elem in l:
        if elem == x:
            b = True
            indice = i
        i = i + 1
    if b:
        return(b,indice)
    else:
        return b

## Exercice 3

def somme(l):
    s = 0
    for x in l:
        s = x+s
    return s

def moyenne(l):
    return somme(l)/len(l)

## Exercice 4

def echange(l):
    ll = l[:]
    ll[0],ll[-1] = ll[-1],ll[0]
    return(ll)

## Exercice 5

def miroir(l):
    n = len(l)
    mir = []
    for i in range(n):
        mir.append(l[n-i-1])
    return mir

def miroir2(l):
    n = len(l)
    ll = l[:]
    for i in range(n//2):
        ll[i],ll[n-1-i] = ll[n-1-i],ll[i]
    return ll

## Exercice 6

def list_fibo(n):
    u,v=0,1
    l=[]
    for i in range(n+1):
        l.append(u)
        u,v=v,u+v
    return l

## Exercice 7
print([i*i for i in range(0,12) if i%2 ==0])

## Exercice 8
def sommes_possibles(n):
    return [(a,b,a+b) for a in range(n) for b in range(n)]

def sommes_possibles2(n):
    l = []
    for a in range(n):
        for b in range(n):
            l.append((a,b,a+b))
    return l

## Exercice 9
def premiere_lettre(l):
    return [s[0] for s in l]

def premiere_lettre2(l):
    l = []
    for s in l:
        l.append(s[0])
    return l

## Exercice 10
import random as rd

def entiers_alea(n,p):
    return [rd.randint(0,p) for _ in range(n)]

def entiers_alea2(n,p):
    l = []
    for i in range(n):
        l.append(rd.randint(0,p))
    return l

def absents(p,l):
    cmpt = 0
    for x in range(p+1):
        if not x in l:
            cmpt+=1
    return cmpt

def proba_absent(N):
    n = 100
    p = 99
    l = [absents(99,entiers_alea(n,p)) for _ in range(N)]
    return moyenne(l)

def proba_absent2(N):
    n = 100
    p = 99
    proba = 0
    for i in range(N):
        proba = proba + absents(99,entiers_alea(n,p))/N
    return proba