-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhillsAndValleys.py
47 lines (38 loc) · 1.21 KB
/
hillsAndValleys.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
def hills(nums):
if len(nums) < 3: return 0
left, right = nums[0], 0
hills = 0
for i in range(1, len(nums) - 1):
if nums[i] != nums[i - 1]:
left = nums[i - 1]
right = next((x for x in nums[i+1:] if x != nums[i]), None)
curr = nums[i]
if nums[i] != nums[i - 1]:
if right and curr > left and curr > right:
hills += 1
return hills
def valleys(nums):
if len(nums) < 3: return 0
left, right = nums[0], 0
valleys = 0
for i in range(1, len(nums) - 1):
if nums[i] != nums[i - 1]:
left = nums[i - 1]
right = next((x for x in nums[i+1:] if x != nums[i]), None)
curr = nums[i]
if nums[i] != nums[i - 1]:
if right and curr < left and curr < right:
valleys += 1
return valleys
arr = [1,2,1]
assert(hills(arr) == 1) # returns 1
assert(valleys(arr) == 0) # returns 0
arr = [1,0,1]
assert(hills(arr) == 0) # returns 0
assert(valleys(arr) == 1) # returns 1
arr = [7,6,5,5,4,1]
assert(hills(arr) == 0) # returns 0
assert(valleys(arr) == 0) # returns 0
arr = [3,4,1,1,6,5]
assert(hills(arr) == 2) # returns 2
assert(valleys(arr) == 1) # returns 1