Skip to content

Commit ab2fb81

Browse files
committed
Submitting lab8
1 parent 47bb53b commit ab2fb81

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Code/renan/lab8.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
# P P
3+
data = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 5, 6, 7, 8, 9, 8, 7, 6, 7, 8, 9]
4+
# 0 , 1, 2, 3, 4, 5, 6, 7, 8, 9,
5+
#peaks - Returns the indices of peaks. A peak has a lower number on both the left and the right.
6+
def peaks(data):
7+
peak_indices = []
8+
#loop over data (1:-1 keeps the loop between index 2 and 8 for the peaks n valleys...to analyze left and right..)
9+
for i in range(1, len(data) -1):
10+
# compare current index with previous and next values
11+
next = data[i + 1]
12+
previous = data[i -1]
13+
current = data[i]
14+
#if previous and next are both lower value than current, we found a peak
15+
if previous < current > next:
16+
17+
peak_indices.append(i)
18+
19+
return peak_indices
20+
21+
print("Found A Peak")
22+
23+
# print(peaks())
24+
25+
#valleys - return the indices of valleys.
26+
def valleys():
27+
valley_indices = []
28+
29+
for i in range(1, len(data) -1):
30+
31+
if data[i- 1] > data[i] < data[i +1]:
32+
valley_indices.append(i)
33+
34+
# if(value[data] < value[data-1] and value[data] < value[data + 1]):
35+
# print
36+
#
37+
return valley_indices
38+
39+
print(peaks(data))
40+
print(valleys())
41+
42+
43+
44+
45+
def peaks_and_valleys():
46+
p_indices = peaks(data)
47+
v_indices = valleys()
48+
49+
peaks_valleys = p_indices + v_indices
50+
51+
peaks_valleys.sort()
52+
return peaks_valleys
53+
54+
def print_peaks_and_valleys():
55+
rows = max(data)
56+
cols = len(data)
57+
for i in range(rows, 0, -1):
58+
for j in range(cols):
59+
if i > data[j]:
60+
print(' ', end=" ")
61+
else:
62+
print('X', end=" ")
63+
print()
64+
65+
print(data)
66+
67+
print_peaks_and_valleys()

0 commit comments

Comments
 (0)