-
Notifications
You must be signed in to change notification settings - Fork 1
/
EXAMEN_Difficile_Vallee.py
62 lines (44 loc) · 1.32 KB
/
EXAMEN_Difficile_Vallee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def maximaLocaux(tab):
res = []
prev = 0
croissant = False
for i in range(len(tab)):
v = tab[i]
if v >= prev:
croissant = True
else:
if croissant:
res.append(i-1)
croissant = False
prev = v
return res
def estVallee(tab, xa, xb):
ya = tab[xa]
yb = tab[xb]
for x in range(xa, xb+1):
y = tab[x]
if y > valeurDroite(xa, ya, xb, yb, x):
return False
return True
def valeurDroite(xa, ya, xb, yb, x):
return ((x - xa) * (ya - yb)) / (xa - xb) + ya
def plusGrandeVallee(tab):
# Les plus grandes vallees sont nécessairement entre les maxima locaux...
maxima = maximaLocaux(tab)
maxValleeX = 0
maxValleeY = 0
for i in range(len(maxima)):
for j in range(i+1, len(maxima)):
x = maxima[i]
y = maxima[j]
if estVallee(tab, x, y) and (length(x,y) > length(maxValleeX, maxValleeY)):
maxValleeX = x
maxValleeY = y
return maxValleeX, maxValleeY
def length(x,y):
return abs(x-y)
# ----------------------------------------------------
tab = [3, 6, 7, 10, 13, 11, 11, 12, 10, 9, 7, 5, 4, 3, 3, 4, 3, 4, 2, 1, 2]
print(maximaLocaux(tab))
print(estVallee(tab, 7, 15))
print(plusGrandeVallee(tab))