aboutsummaryrefslogtreecommitdiff
path: root/challenge-218/robert-dicicco/python/ch-1.py
blob: 0cb536308722981eb25209916cf364686a525c48 (plain)
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
#!/usr/bin/env python

# -----------------------------------------
# AUTHOR: Robert DiCicco
# DATE  : 2023-05-22
# Challenge 218 Maximum Product ( Python )
# -----------------------------------------

from itertools import combinations


lists = [[3, 1, 2],[4, 1, 3, 2],[-1, 0, 1, 3, 1],[-8, 2, -9, 0, -4, 3]]

maxthree = []

for lst in lists:
    print("Input: @list = ",lst)
    maxval = 0
    maxthree = []
    for combo in list(combinations(lst,3)):
        prod = combo[0] * combo[1] * combo[2]
        if prod > maxval:
            maxval = prod
            maxthree = combo
    print("Output: ",maxval)
    maxthree = sorted(maxthree)
    print(maxthree[0]," x ", maxthree[1], " x ", maxthree[2], " = ",maxval,"\n")
    maxval = 0

# -----------------------------------------
# SAMPLE OUTPUT
# python .\MaxProduct.py

# Input: @list =  [3, 1, 2]
# Output:  6
# 1  x  2  x  3  =  6

# Input: @list =  [4, 1, 3, 2]
# Output:  24
# 2  x  3  x  4  =  24

# Input: @list =  [-1, 0, 1, 3, 1]
# Output:  3
# 1  x  1  x  3  =  3

# Input: @list =  [-8, 2, -9, 0, -4, 3]
# Output:  216
# -9  x  -8  x  3  =  216
# -----------------------------------------