-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax-triplet.py
49 lines (37 loc) · 1 KB
/
max-triplet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 8 20:32:45 2020
@author: artemponomarev
"""
A = [-3, 1, 2, -2, 5, 6]
def solution(A):
"""
Task description is given on Codility website, https://app.codility.com/programmers/
maximum product among all triplets
"""
A.sort()
if max(A) < -1000 and max(A) > 1000:
return 0
if len(A) < 3 or len(A) > 100000:
return 0
if len(A) == 3:
return A[0]*A[1]*A[2]
product = []
if A[-1] > 0 and A[-2] > 0 and A[-3] > 0:
product.append(A[-1]*A[-2]*A[-3])
if A[0] < 0 and A[1] < 0 and A[-1] >= 0:
product.append(A[0]*A[1]*A[-1])
if A[0] < 0 and A[1] >= 0:
if A[1] == 0:
product.append(0)
else:
product.append(A[0]*A[1]*A[2])
if A[-1] <= 0 and A[-2] <= 0 and A[-3] <= 0:
product.append(A[-1]*A[-2]*A[-3])
if product:
return max(product)
else:
return -1
# main()
print("result = ", solution(A))