1
1
import unittest
2
2
3
+
3
4
def get_max_profit (stock_prices_yesterday ):
4
5
"""Uses greedy approach. A greedy algorithm iterates through the problem space taking the optimal solution "so far,"
5
6
until it reaches the end.
@@ -22,6 +23,7 @@ def get_max_profit(stock_prices_yesterday):
22
23
min_price = min (min_price , current_price )
23
24
return max_profit
24
25
26
+
25
27
def get_product_of_all_ints_except_at_index (int_list ):
26
28
# Create list
27
29
products = [None ] * len (int_list )
@@ -36,35 +38,26 @@ def get_product_of_all_ints_except_at_index(int_list):
36
38
cum_product *= int_list [i ]
37
39
# Product of all numbers after int
38
40
after_product = int_list [- 1 ]
39
- for j in range (2 ,len (int_list )+ 1 ):
41
+ for j in range (2 , len (int_list )+ 1 ):
40
42
print ("after: " , len (int_list )+ 1 - j , after_product )
41
43
products [- j ] *= after_product
42
44
after_product *= int_list [- j ]
43
45
print (products )
44
46
return products
45
47
46
48
47
- def my_function (arg ):
48
- # write the body of your function here
49
- return 'running with %s' % arg
50
-
51
-
52
- # run your function through some test cases here
53
- # remember: debugging is half the battle!
54
- print
55
- my_function ('test input' )
56
-
57
-
58
49
def product (list_of_ints ):
59
- pass
50
+ return list_of_ints [ 0 ] * list_of_ints [ 1 ] * list_of_ints [ 2 ]
60
51
61
52
62
53
def max_three (list_of_ints ):
63
- pass
54
+ sorted_list_of_ints = sorted (list_of_ints )
55
+ return sorted_list_of_ints [:3 ]
64
56
65
57
66
58
def min_three (list_of_ints ):
67
- pass
59
+ sorted_list_of_ints = sorted (list_of_ints )
60
+ return sorted_list_of_ints [- 3 :]
68
61
69
62
70
63
def highest_product_from_list_of_ints (list_of_ints ):
@@ -76,18 +69,59 @@ def highest_product_from_list_of_ints(list_of_ints):
76
69
return product (min_three (list_of_ints ))
77
70
# If some are positive and some are negative
78
71
else :
72
+ # Sort in ascending order
79
73
sorted_list_of_ints = sorted (list_of_ints )
80
-
81
- max_1 = max (list_of_ints )
82
- max_2 = second_max (list_of_ints )
83
- min_1 = min (list_of_ints )
84
- # min_2 =
85
-
74
+ # We want one positive and
75
+ # two neg or two pos, depending on which product is larger.
76
+ if sorted_list_of_ints [1 ] < 0 or sorted_list_of_ints [- 3 ] > 0 :
77
+ max_1 = sorted_list_of_ints [- 1 ]
78
+ max_pos = sorted_list_of_ints [- 2 ] * sorted_list_of_ints [- 3 ]
79
+ max_neg = sorted_list_of_ints [0 ] * sorted_list_of_ints [1 ]
80
+ max_2_3 = max (max_pos , max_neg )
81
+ if max_2_3 > 0 :
82
+ return max_1 * max_2_3
83
+ elif 0 in list_of_ints :
84
+ return 0
85
+ else :
86
+ # The remaining alternative: Product is negative.
87
+ # Precisely one negative number, two positive numbers and no zeroes.
88
+ # That means we have precisely three integers. This case was covered.
89
+ return "Error"
90
+
91
+ def highest_product_from_list_of_ints_greedy (list_of_ints ):
92
+ highest_product_of_three = list_of_ints [0 ] * list_of_ints [1 ] * list_of_ints [2 ]
93
+ if list_of_ints [3 ] is None :
94
+ return highest_product_of_three
95
+ highest = list_of_ints [0 ]
96
+ lowest = list_of_ints [0 ]
97
+ max_product_two = list_of_ints [0 ] * list_of_ints [1 ]
98
+ min_product_two = list_of_ints [0 ] * list_of_ints [1 ]
99
+ # Go through list
100
+ for int in list_of_ints :
101
+ highest_product_of_three = max (int * highest , int * min_product_two , highest_product_of_three )
102
+ max_product_two = max (highest * int , lowest * int , max_product_two )
103
+ min_product_two = min (lowest * int , lowest * int , min_product_two )
104
+ highest = max (highest , int )
105
+ lowest = min (lowest , int )
106
+ return highest_product_of_three
107
+
108
+ # LOGIC:
109
+ # PPP -> max*max_product_two
110
+ # PPN -> min*max_product_two
111
+ # PNN -> min*min_product_two
112
+ # NNN -> max*min_product_two
113
+ # NN...0 or PPN0 -> the 0 kicks in during highest_product_of_three = max(...)
86
114
87
115
class Tests (unittest .TestCase ):
88
116
def test_p2_get_product_of_all_ints_except_at_index (self ):
89
- numbers = [1 ,2 ,3 ]
90
- self .assertEqual (get_product_of_all_ints_except_at_index (numbers ), [6 ,3 ,2 ])
117
+ numbers = [1 , 2 , 3 ]
118
+ self .assertEqual (get_product_of_all_ints_except_at_index (numbers ), [6 , 3 , 2 ])
119
+
120
+ def test_highest_product_from_list_of_ints (self ):
121
+ self .assertEqual (highest_product_from_list_of_ints ([10 , 2 , 0 , 3 , - 5 ]), 60 )
122
+ self .assertEqual (highest_product_from_list_of_ints ([1000 , 2 , 0 , - 100 ]), 0 )
123
+ self .assertEqual (highest_product_from_list_of_ints ([- 10 , - 5 , 1 , 2 , 3 , 4 ]), 200 )
124
+ self .assertEqual (highest_product_from_list_of_ints ([- 10 , - 10 , 1 , 3 , 2 ]), 300 )
91
125
92
126
if __name__ == '__main__' :
93
- unittest .main ()
127
+ unittest .main ()
0 commit comments