-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_pairwise_product.py
More file actions
39 lines (31 loc) · 1.1 KB
/
max_pairwise_product.py
File metadata and controls
39 lines (31 loc) · 1.1 KB
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
import time
def max_pairwise_product(number_list):
result = 0
for i in range(len(number_list) - 1):
product = number_list[i] * number_list[i + 1]
if product > result:
result = product
return result
def max_pairwise_product_fast(numbers):
max_index_1 = max_index(numbers)
max_index_2 = max_index(numbers, max_index_1)
return max_index_1 * max_index_2
def max_index(numbers, skip_index=-1):
top = 0
for index in range(len(numbers) - 1):
if numbers[index] > numbers[index + 1]:
top = i
return top
if __name__ == "__main__":
input_list = []
elements = 100
for i in range(elements):
input_list.append(i + 1)
start = time.perf_counter()
result = max_pairwise_product(input_list)
elapsed_time = time.perf_counter() - start
print("Result {}, Elapsed time {:0.2f} secs".format(result, elapsed_time))
start = time.perf_counter()
result = max_pairwise_product_fast(input_list)
elapsed_time = time.perf_counter() - start
print("Result {}, Elapsed time {:0.2f} secs".format(result, elapsed_time))