-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-peak.py
executable file
·55 lines (42 loc) · 1.43 KB
/
6-peak.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
#!/usr/bin/python3
"""
Technical Interview Prep:
Find a peak via divide and conqure
"""
def find_peak(list_of_integers):
""" Wrapper function for reqursion
Args:
list_of_integers (list): list of unsorted integers
Returns:
int: local max
"""
size = len(list_of_integers)
if size == 0: # list is empty
return None
if size == 1: # only one number in list
return list_of_integers[0]
# recurse via divide and conqure helper function
return divide_conquer(list_of_integers, 0, size - 1)
# indexed
def divide_conquer(array, start, end):
""" Recursion helper function
Args:
array (list): list of integers
start (int): index of starting location
end (int): index of ending location
Returns:
int: local max
func: recursive call to function
"""
mid = int((start + end) / 2)
if start < mid < end: # make sure more than 2 numbers
if array[mid - 1] < array[mid] > array[mid + 1]: # peak found
return array[mid]
if array[mid - 1] > array[mid]: # left > pointer -> d&c left side
return divide_conquer(array, start, mid)
else: # d&c right side
return divide_conquer(array, mid, end)
if array[start] > array[end]: # only 2 number return the greater one
return array[start]
else:
return array[end]