#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 28 21:30:16 2017

@author: Alain
"""
from math import sqrt
import time


""" Exercice 1.1 """

L1 = []
for i in range(0,11):
    L1.append(i)
# ou L1 = [i for i in range(0,11)], idem pour les suivantes

L2 = []
for i in range(10,-12,-2):
    L2.append(i)

L3 = []
x = 0
while x <= 10 : 
    L3.append(x)
    x = x + 0.1

# ou
#L3 = []
#for i in range(0,101):
#    L3.append(i/10)
   
L4 = []
for i in range(ord("a"), ord("a")+26):
    L4.append(chr(i))
print(L4)


""" Exercice 1.2 """

Eleves = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
Trim1 = [10, 12, 9, 14, 8, 15, 18, 13, 6, 10]
Trim2 = [11, 10, 8, 16, 12, 16, 16, 11, 8, 9]

n = 0
for i in range(len(Trim1)):
    if Trim2[i] > Trim1[i]:
        n = n + 1
print ("Nombre de progrès : ", n)

s = 0
for note in Trim1 : 
    s = s + note
moyenne =  s / len(Trim1)
print("Moyenne : ", moyenne)

maxi = Trim1[0]
for i in range(1, len(Trim1)):
    if Trim1[i]> maxi : 
        maxi = Trim1[i]
print("Meilleure note : ", maxi)

maxi = Trim1[0]
indice = 0
for i in range(1, len(Trim1)):
    if Trim1[i]> maxi : 
        maxi = Trim1[i]
        indice = i
print("Meilleure note : ", maxi, " pour ", Eleves[indice])

maxi = Trim2[0]
best = [Eleves[0]]
for i in range(1, len(Trim2)):
    if Trim2[i] == maxi : 
        best.append(Eleves[i])
    elif Trim2[i] > maxi :
        maxi = Trim2[i]
        best = [Eleves[i]]
print("Meilleure note : ", maxi, " pour ", best)

""" Exercice 2.1 """

N = 10000

start = time.time()
P = []
for i in range (2,N+1):
    trouve = False 
    j = 0
    while j < len(P) : 
        if i%P[j] == 0:
            trouve = True
        j = j + 1
    if not trouve : 
        P.append(i)
interval = time.time() - start
print (P)
print (interval)

start = time.time()
E = [0,0]# 0 et 1 ne sont pas premiers !
for i in range(2, N+1):
    E.append(i)
for i in range(2, int(sqrt(N))+1):
    if E[i] != 0 : # c'est un nombre premier
        for j in range(2*i, N+1, i):
            E[j]= 0 
Pr = []
for i in range(N+1):
    if E[i]!= 0 :
        Pr.append(i)
interval = time.time() - start
print (Pr)
print (interval)

    
""" Exercice 2.2 """   


for i in range(len(Trim1)):
    mini = Trim1[i]
    indice = i
    for j in range(i+1, len(Trim1)):
        if Trim1[j] < mini : 
            mini = Trim1[j]
            indice = j
    tmp = Trim1[i]
    Trim1[i] = mini
    Trim1[indice] = tmp
print (Trim1)


    
""" Réactions successives """


import random as rd
import matplotlib.pyplot as plt

#parmètres du système
N = 10000
p1 = 0.90
p2 = 0.95
tmax = 100

#initialisation de l'automate et des tableaux
Automate = [0 for i in range(N)]
T = [0]
A = [N]
B = [0]
C = [0]

t = 0
while t < tmax :
    #on gère les transitions
    for i in range(N):
        x = rd.random()#nombre aléatoire entre 0 et 1
        if Automate[i] == 0 and x > p1 :
            Automate[i] = 1 
        elif Automate[i] == 1 and x > p2 :
            Automate[i] = 2
    #on compte le nombre d'états
    nbA = 0
    nbB = 0
    nbC = 0
    for i in range(N):
        if Automate[i]==0 :
            nbA = nbA+1
        elif Automate[i]==1 : 
            nbB = nbB+1
        else :
            nbC = nbC + 1  
    # on n'oublie pas d'incrémenter le temps
    t = t+1
    #on stocke les valeurs 1 fois sur 10
    T.append(t)
    A.append(nbA)
    B.append(nbB)
    C.append(nbC)
    
plt.plot(T, A, 'r')
plt.plot(T, B, 'g')
plt.plot(T, C, 'b')
plt.show()

