Skip to content

Commit 07efef5

Browse files
author
Jessica Yung
committed
feat(interviewcake): p3 soln and greedy soln draft
1 parent 1970c97 commit 07efef5

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

interview-cake/p1_to_p5.py

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
34
def get_max_profit(stock_prices_yesterday):
45
"""Uses greedy approach. A greedy algorithm iterates through the problem space taking the optimal solution "so far,"
56
until it reaches the end.
@@ -22,6 +23,7 @@ def get_max_profit(stock_prices_yesterday):
2223
min_price = min(min_price, current_price)
2324
return max_profit
2425

26+
2527
def get_product_of_all_ints_except_at_index(int_list):
2628
# Create list
2729
products = [None] * len(int_list)
@@ -36,35 +38,26 @@ def get_product_of_all_ints_except_at_index(int_list):
3638
cum_product *= int_list[i]
3739
# Product of all numbers after int
3840
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):
4042
print("after: ", len(int_list)+1-j, after_product)
4143
products[-j] *= after_product
4244
after_product *= int_list[-j]
4345
print(products)
4446
return products
4547

4648

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-
5849
def product(list_of_ints):
59-
pass
50+
return list_of_ints[0] * list_of_ints[1] * list_of_ints[2]
6051

6152

6253
def max_three(list_of_ints):
63-
pass
54+
sorted_list_of_ints = sorted(list_of_ints)
55+
return sorted_list_of_ints[:3]
6456

6557

6658
def min_three(list_of_ints):
67-
pass
59+
sorted_list_of_ints = sorted(list_of_ints)
60+
return sorted_list_of_ints[-3:]
6861

6962

7063
def highest_product_from_list_of_ints(list_of_ints):
@@ -76,18 +69,59 @@ def highest_product_from_list_of_ints(list_of_ints):
7669
return product(min_three(list_of_ints))
7770
# If some are positive and some are negative
7871
else:
72+
# Sort in ascending order
7973
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(...)
86114

87115
class Tests(unittest.TestCase):
88116
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)
91125

92126
if __name__ == '__main__':
93-
unittest.main()
127+
unittest.main()

0 commit comments

Comments
 (0)