##############################
##########   TP 03 ###########
##############################

from random import random,randint

## Ex 1 :
# Q1
N = 10
L = []
for k in range(N):
    p = random()  # tirage alea [0,1]
    if p > .5 :
        L.append(1)
    else :
        L.append(0)
print(L) # on affiche L pour contrôler

# pour les guerriers, par compréhension :
L  = [1 * ( random()>0.5 ) for k in range(N)]
# on triche : 1*False = 0




# Q2 Calcul de la moyenne
m = 0
for x in L :  # parcours par itérateur
    m += x
m /= len(L)
# ou  :
m=0
for k in range(len(L)): # parcours par indice
    m += L[k]
m /= len(L)
print("moyenne : ",m)


# Q3
c = 0
for k in range(len(L)-1):
    if L[k]==1 and L[k+1]==1 :
        c += 1
# ou
c = 0
for k in range(len(L) -1 ):
    if L[k] + L[k+1]==2 :
        c += 1
print(c)


# Q4
c = 0
n = 20
for k in range(len(L) - n ):
    # décompte sur le nbre de 1 successifs
    nbre = 0
    for j in range(n):
        nbre += L[k+j]
    # nbre  = somme de L[k] à L[k+n-1]
    if nbre ==n:
        c +=1
print(c)

## exercice 2
N = 10
L = []
for _ in range(N):
    L.append(randint(1,6))
print(L)

# nbre de 6 ?
c6 = 0
for x in L :
    if x==6:
        c6 += 1
print("nbre de 6 :",c6)

#Q3
C = [0,0,0,0,0,0] # liste de compteurs [c1,c2,..,c6]
for face in L :
    i_C = face-1 # indice commence à 0
    C[i_C] +=1

for k in range(6):
    print('la valeur',k+1,'apparait',C[k],'fois')

indices_6 = []
for i in range(len(L)):
    if L[i] == 6:
        indices_6.append(i)
print(indices_6)

periode_6 = []
for i in range(len(indices_6)-1):
    periode_6.append(indices_6[i+1]-indices_6[i])
print(periode_6)

Knuth = [2]
for i in range(1,30):
    Knuth.append((137*Knuth[i-1]+187)%256)
print(Knuth)

## Trouve mon nombre

nombre = randint(0,100)
trouve = False
c = 0
for i in range(6):

    if not(trouve):
        c += 1
        n = int(input("Proposez un entier"))
        if n == nombre:
            print("Gagné !")
            trouve = True
        elif n > nombre:
            print("Le nombre proposé est trop grand")
        else:
            print("Le nombre proposé est trop petit")
if c == 6:
    print("Perdu")

## Ex 4

u = 17
n = 20
for i in range(1, n+1):
    if u % 2 == 0:
        u = u // 2
    else:
        u = 3 * u + 1
    print(u)

## Ex 5

def scd_degre(a,b,c):
    if a == 0 and b == 0:
        if c == 0:
            return "infinité"
        else:
            return "aucune"
    elif a == 0 and b != 0:
        return "une"
    else:
        delta = b**2-4*a*c
        if delta == 0:
            return "une"
        elif delta > 0:
            return "deux"
        else:
            return "aucune"

## Ex 6

def bissextile(annee):
    if (annee % 4 == 0 and annee % 100 != 0) or (annee % 400 == 0):
        return True
    else:
        return False

## Ex 7

def passe_valide(mot):
    maj = False #par défaut il n'y a pas de majuscule
    min = False#par défaut il n'y a pas de minuscule
    chiffre = False #par défaut il n'y a pas de chiffre
    spec = False #par défaut il n'y a pas de caractère spécial
    alph = "#@*+-;:%*$€"
    for lettre in mot:
        if lettre.isupper():
            maj = True #il y a au moins une majuscule
        if lettre.islower():
            min = True # il y a au moins une minuscule
        if lettre in alph:
            spec = True # il y a au moins un caractère spécial
        if lettre in "0123456789":
            chiffre = True # il y a au moins un chiffre
    return maj and min and spec and chiffre and len(mot) > 7 # on teste aussi la longueur du mot de passe

## Plateau de jeu aléatoire

def ligne_alea(N,p):
    s = ""
    for i in range(N):
        x = random()
        if x > p:
            s += "-"
        else:
            s += "o"
    return s

print(ligne_alea(10,0.8))

def gen_plateau(N,p):
    for i in range(N):
        print(ligne_alea(N,p))

print(gen_plateau(10,0.8))




