-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpairwise.py
104 lines (70 loc) · 2.09 KB
/
pairwise.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#python 3
'''
Resources: https://stackoverflow.com/questions/126524/iterate-a-list-with-indexes-in-python
'''
from random import randint
'''
Original pairwise product algorithm
Slowest run time
'''
def max_pairwise_product(numbers):
n = len(numbers)
max_product = 0
for first in range(n):
for second in range(first + 1, n):
max_product = max(max_product,
numbers[first] * numbers[second])
return max_product
'''
Faster pairwise product algorithm
faster runtime
'''
def pairwise1(numbers):
max_index1 = 0
max_index2 = 0
#find the highest number
for i, val in enumerate(numbers):
if int(numbers[i]) > int(numbers[max_index1]):
max_index1 = i
if max_index1 == 0:
max_index2 = 1
else:
max_index2 = 0
#find the second highest number
for j, val in enumerate(numbers):
if j!=max_index1 and int(numbers[j]) > int(numbers[max_index2]):
max_index2 = j
#print(max_index1)
#print(max_index2)
return int(numbers[max_index1]) * int(numbers[max_index2])
'''
Another solution to the pairwise product
Even faster run time (probably)
'''
def pairwise(arr):
arr.sort(reverse = True)
solution = int(arr[0]) * int(arr[1])
return solution
def stressTest():
while True:
arr = []
for x in range(5):
random_num = randint(2,101)
arr.append(random_num)
print(arr)
print('####')
result1 = max_pairwise_product(arr)
result2 = pairwise1(arr)
print("Result 1 {}, Result2 {}".format(result1,result2))
if result1 != result2:
print("wrong answer: {} **** {}".format(result1, result2))
break
else:
print("############################################# \n Ok", result1, result2)
if __name__ == '__main__':
#stressTest()
length = input()
numbers = [int(x) for x in input().split()]
answer = pairwise(numbers)
answer = pairwise1(numbers)
print(answer)