#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct  4 08:06:51 2019

@author: suwako
"""

# Structures de données

a = [0, 1, 'abc', 4.5, 'de', 6]  # un niveau
b = [[], [1], [1, 2], [1, 2, 3]]  # deux niveaux

a[2]  # On attend 'abc'
b[3][1]  # On attend 2

a[1:5]  # Slicing
a[1:-1]  # La même chose
a[:4]  # On peut ommetre une partie
a[-3:]
li = list(range(11))  # Création d'une liste à partir d'une énumération
li[2:9:3]  # On peut rajouter un pas
li[3::2]
li[:-1:2]
li[-1:0:-1]

list("Louis-Le-Grand")

[x for x in range(11) if x * x <= 50]

a, b = [1, 2, 3], [2, 4, 6]
[(x, y) for x in a for y in b]
[(x, y, z) for x in range(1, 20) for y in range(x, 20) for z in range(y, 20) if x**2 + y**2 == z**2]

# Concaténation
[2, 4, 6, 8] + [1, 3, 5, 7]

# Duplication
[1, 2, 3]*3

# Mutation
li = list(range(11))
li[3] = 'a'
li[3:7] = 'abc'  # On perd un élément ici
li

li = list(range(10))
li[1::2] = li[0::2]
li

li = list(range(11))
del li[3:6]  # Suppression d'élements
li

li = ['a', 'b', 'c', 'd']
li.append('e')  # Ajout d'élément
li
li.insert(2, 'x')  # Insertion d'élément
li

li = list(range(1, 6)) * 2
li.remove(4)  # On retire la première occurence de 4
li

li.pop(-1) # Affiche le dernier élement et le retire de la liste
li

li = list(range(1, 4)) * 2
li.reverse()  # On renverse la liste ; Attention, une méthode modifie l'objet
li

li.sort()  # On trie la liste par ordre croissant
li

sorted(li)  # Ici, on trie également, mais on ne modifie pas la liste originale
li

list('abcde')  # Transformation de type
str(list('abcde'))
tuple([1, 2, 3, 4])

# Les listes sont mutables
lst = [1, 2, 3]
lst[0] = 4
lst

tup = (1, 2, 3)
tup[0] = 4
chr = 'abc'
chr[0] = 'd'

l1 = [1, 2, 3]
id(l1)
tup1 = (1, 2, 3)
id(tup1)
l1[0] = 4
id(l1)
tup1 = (4, 2, 3)
id(tup1)

l2 = l1
id(l1) == id(l2)
tup2 = tup1
id(tup1) == id(tup2)

l1[0] = 1
l2
tup1 = (1, 2, 3)
tup2

l3 = l1[:]
l3 = l1.copy()
l3 = [x for x in l1]

l1 = [[1, 2, 3], [4, 5, 6]]
l2 = l1[:]
id(l1) == id(l2)
id(l1[0]) == id(l2[0])

from copy import deepcopy
l3 = deepcopy(l1)
id(l1) == id(l3)
id(l1[0]) == id(l3[0])

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

def variance(l):
    n = len(l)
    s = c = 0
    for x in l:
        s += x
        c += x * x
    return c/n - (s/n) * (s/n)

def maximum(l):
    m = l[0]
    for x in l:
        if x > m:
            m = x
            return m
def indice_max(l):
    i, m = 0, l[0]
    for k in range(1, len(l)):
        if l[k] > m:
            i, m = k, l[k]
    return i

def maximum(l):
    m = l[0]
    for x in l:
        if x > m:
            m = x
    return m

def indice_du_max(l):
    i, m = 0, l[0]
    for (k, x) in enumerate(l):
        if x > m:
            i, m = k, x
    return i
def cherche(x, l):
    k, rep = 0, False
    while k < len(l) and not rep:
        rep = l[k] == x
        k += 1
    return rep
def cherche(x, l):
    for y in l:
        if y == x:
            return True
    return False

(x in l)

def cherche_dicho(x, l):
    i, j = 0, len(l)
    while i < j:
        k = (i + j) // 2
        if l[k] == x:
            return True
        elif l[k] > x:
            j = k
        else:
            i = k + 1
    return False
