## TP6 -- Exercices sur les listes



## Exercise 6.1 -- Création d'une liste constante de taille donnée

# Voici une première solution :
def listeConstante(n, a):
	L = []
	for i in range(n):
		L.append(a)
	return L
	
# Voici une autre solution :
def listeConstante2(n, a):
	return [a]*n

	
# Voici une dernière solution :
def listeConstante3(n, a):
	return [a for i in range(n)]



## Exercise 6.2 -- Une suite

def premiersTermes(n):
	L = [1]
	for i in range(n):
		u_precedent = L[-1]
		u_suivant = -2*u_precedent + 1
		L.append(u_suivant)
	return L



## Exercise 6.3 -- Création de listes par compréhension

## question 1)

import math as mt

# Voici une première solution :
def selectionSinus(n):
	L = []
	for k in range(n+1):
		x = mt.sin(k)
		if x >= 0:
			L.append(x)
	return L

# Voici une solution utilisant des listes créées par compréhension :
def selectionSinus1(n):
	return [mt.sin(k) for k in range(n+1) if mt.sin(k) >= 0]

## question 2)

import math as mt

# Voici une première solution :
def indicesSelectionSinus(n):
	L = []
	for k in range(n+1):
		x = mt.sin(ki)
		if x >= 0:
			L.append(k)
	return L

# Voici une solution utilisant des listes créées par compréhension :
def indicesSelectionSinus1(n):
	return [k for k in range(n+1) if mt.sin(k) >= 0]



## Exercise 6.4 -- Détection de doublons

## question 2)

def possedeDoublon(l):
	n = len(l)
	for i in range(n):
		for j in range(i+1, n):
			if l[i] == l[j]:
				return True
	return False



## Exercise 6.5 -- Remplacement d'un élément

## question 2)

def remplace(l, x, y):
	n = len(l)
	for i in range(n):
		if l[i] == x:
			l[i] = y



## Exercise 6.6 -- Nettoyage des zéros finaux

## question 2)

def nettoie(l):
	while len(l) > 0
		if l[-1] == 0:
			l.pop()
		else:
			return



## Exercise 6.7 -- Listes des indices où le maximum est atteint

## question 2)

def indicesMaximaux(l):
	n = len(l)
	if n == 0:
		return []
	
	record = l[0]
	indicesRecord = [0]
	
	for i in range(n):
		x = l[i]
		if x == record:
			indicesRecord.append(i)
		elif x > record:
			record = x
			indicesRecord = [i]
	
	return indicesRecord
	
# Voilà une solution plus savante, utilisant float("inf")
# et permettant d'avoir un code plus synthétique
def indicesMaximaux1(l):
	n = len(l)
	record = - float("inf")
	indicesRecord = []
	
	for i in range(n):
		x = l[i]
		if x == record:
			indicesRecord.append(i)
		elif x > record:
			record = x
			indicesRecord = [i]
	
	return indicesRecord



## Exercise 6.8 -- Pénultième plus grand élément

## question 2)

def penultieme(l):
	if l == []:
		return None
	plusGrand = l[0]
	avantPlusGrand = None
		
	for i in range(n):
		x = l[i]
		if x > plusGrand:
			avantPlusGrand = plusGrand
			plusGrand = x
	
	return avantPlusGrand



## Exercise 6.9 -- Tri d'une liste

## question 2)

# On va coder le tri par sélection.

# La fonction suivante renvoie l'indice d'un élément
# minimal de l dont l'indice est >= à i
def indiceMinimum(l, i):
	n = len(l)
	resultat = i
	for j in range(i+1, n):
		if l[j] < l[resultat]:
			resultat = j
	return resultat
		
def listeTriee(l):
	L = l[:]
	n = len(L)
	for i in range(n):
		indiceMin = indiceMinimum(L, i)
		L[i], L[indiceMin] = L[indiceMin], L[i]
	return L
