-
Notifications
You must be signed in to change notification settings - Fork 0
/
highest-product.py
32 lines (28 loc) · 999 Bytes
/
highest-product.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
"""
Given a list_of_ints, find the highest_product you can get from three of the
integers.The input list_of_ints will always have at least three integers.
"""
#works for positive integers
def highest_product(list_of_numbers):
if len(list_of_numbers)<3:
print 'You gotta be kidding me'
exit()
if len(list_of_numbers)==3:
return list_of_numbers[0]*list_of_numbers[1]*list_of_numbers[2]
max1 = max2 = max3 = None
for item in list_of_numbers:
if max1 == None or max1<item:
max1=item
list_of_numbers.pop(list_of_numbers.index(max1))
for item in list_of_numbers:
if max2 == None or max2<item:
max2=item
list_of_numbers.pop(list_of_numbers.index(max2))
for item in list_of_numbers:
if max3 == None or max3<item:
max3=item
list_of_numbers.pop(list_of_numbers.index(max3))
return max1*max2*max3
list_of_numbers = [1,3,9,6,9]
result = highest_product(list_of_numbers)
print result