148 lines
3.2 KiB
Python
Executable File
148 lines
3.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Created on Thu Feb 6 08:23:46 2020
|
|
|
|
@author: suwako
|
|
"""
|
|
|
|
#Exercice 1
|
|
#a)
|
|
"""La fonction len permet de connaître la durée de la randonnée d'Alice."""
|
|
#b)
|
|
def altmax(alts):
|
|
res = alts[0]
|
|
for i in range(1, len(alts)):
|
|
if alts[i] > res :
|
|
res = alts[i]
|
|
return res
|
|
#c)
|
|
def denivmax(alts):
|
|
res = max(0, alts[0])
|
|
for i, j in enumerate(range(1, len(alts))):
|
|
deniv = alts[j] - alts[i]
|
|
if deniv > res :
|
|
res = deniv
|
|
return res
|
|
|
|
def heure_denivmax(alts):
|
|
res = (max(0, alts[0]), 0)
|
|
for i, j in enumerate(range(1, len(alts))):
|
|
deniv = alts[j] - alts[i]
|
|
if deniv > res[0] :
|
|
res = (deniv, j)
|
|
return res[1]
|
|
|
|
def denivtotal(alts):
|
|
res = max(0, alts[0])
|
|
for i, j in enumerate(range(1, len(alts))):
|
|
deniv = alts[j] - alts[i]
|
|
if deniv > 0:
|
|
res += deniv
|
|
return res
|
|
|
|
def sommets(alts):
|
|
res = []
|
|
for a, b, c in zip(range(len(alts)-2),
|
|
range(1, len(alts) - 1),
|
|
range(2, len(alts))):
|
|
if alts[a]<alts[b] and alts[b]>alts[c]:
|
|
res.append(alts[b])
|
|
return res
|
|
|
|
#Exercice 2
|
|
|
|
def pgplateau(tab):
|
|
grand_plateau = 0
|
|
plateau = 0
|
|
for el in tab:
|
|
if el == 0:
|
|
plateau += 1
|
|
else:
|
|
grand_plateau = max(grand_plateau, plateau)
|
|
plateau = 0
|
|
return grand_plateau
|
|
#Exercice 3:
|
|
import random
|
|
#a)
|
|
def tableau_hasard(n=100): return [random.randint(0,n-1) for i in range(n)]
|
|
#b)
|
|
def absents(tab):
|
|
presents = {tab[0]}
|
|
for el in tab[1:]:
|
|
presents.update({el})
|
|
return(100 - len(list(presents)))
|
|
#c)
|
|
def moyenne(*args):
|
|
n=100
|
|
if args:
|
|
n = args[0]
|
|
somme = 0
|
|
for i in range(n):
|
|
somme += absents(tableau_hasard())
|
|
return somme/n
|
|
|
|
#Exercice 4
|
|
#a)
|
|
def premiere_motie(tab):
|
|
return tab[:len(tab)//2 + 1]
|
|
def deuxieme_motie(tab):
|
|
return tab[len(tab)//2 - 1:]
|
|
def pairs(tab):
|
|
return tab[::2]
|
|
def impairs(tab):
|
|
return tab[1::2]
|
|
#b)
|
|
|
|
def out_shuffle(tab):
|
|
tab[::2], tab[1::2] = tab[:len(tab)//2], tab[len(tab)//2:]
|
|
return tab
|
|
#c)
|
|
def nb_shuffle(n=52, shuffle=out_shuffle):
|
|
ori_tab = list(range(n))
|
|
tab = shuffle(ori_tab.copy())
|
|
i = 1
|
|
while tab != ori_tab:
|
|
shuffle(tab)
|
|
i+=1
|
|
return i
|
|
#On est à 8 pour le out_shuffle
|
|
#d)
|
|
def in_shuffle(tab):
|
|
tab[::2], tab[1::2] = tab[len(tab)//2:], tab[:len(tab)//2]
|
|
return tab
|
|
#On est à 52 pour le in_shuffle
|
|
|
|
#Exercice 5
|
|
def pppremier(n):
|
|
i = n+1
|
|
while True:
|
|
j = 2
|
|
while j*j <= i:
|
|
if i % j == 0:
|
|
break
|
|
j += 1
|
|
if j*j > i:
|
|
return i
|
|
i += 1
|
|
|
|
def premiers1(n=1000):
|
|
premiers=[2]
|
|
while premiers[-1]<1000:
|
|
premiers.append(pppremier(premiers[-1]))
|
|
if premiers[-1] > 1000:
|
|
del premiers[-1]
|
|
return premiers
|
|
|
|
def erathostene(n=1000):
|
|
premiers = list(range(2,1001))
|
|
i=0
|
|
while i<len(premiers):
|
|
j = i+1
|
|
while j<len(premiers):
|
|
if premiers[j] % premiers[i] == 0:
|
|
del premiers[j]
|
|
else:
|
|
j+=1
|
|
i+=1
|
|
return premiers |