Skip to content

Commit 1970c97

Browse files
author
Jessica Yung
committed
feat(interviewcake): add p1 and p2 solutions, p3 in progress
1 parent c7b247d commit 1970c97

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

interview-cake/p1_to_p5.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import unittest
2+
3+
def get_max_profit(stock_prices_yesterday):
4+
"""Uses greedy approach. A greedy algorithm iterates through the problem space taking the optimal solution "so far,"
5+
until it reaches the end.
6+
7+
The greedy approach is only optimal if the problem has "optimal substructure," which means stitching together
8+
optimal solutions to sub-problems yields an optimal solution.
9+
"""
10+
if len(stock_prices_yesterday) < 2:
11+
raise IndexError("Getting a profit requires at least 2 prices.")
12+
min_price = stock_prices_yesterday[0]
13+
max_profit = stock_prices_yesterday[1] - stock_prices_yesterday[0]
14+
for index, price in enumerate(stock_prices_yesterday):
15+
if index == 0:
16+
continue
17+
current_price = price
18+
potential_profit = current_price - min_price
19+
20+
max_profit = max(max_profit, potential_profit)
21+
22+
min_price = min(min_price, current_price)
23+
return max_profit
24+
25+
def get_product_of_all_ints_except_at_index(int_list):
26+
# Create list
27+
products = [None] * len(int_list)
28+
print(products)
29+
cum_product = int_list[0]
30+
print("First int: ", cum_product)
31+
products[0] = 1
32+
# Product of all numbers before int
33+
for i in range(1, len(int_list)):
34+
print("before: ", i, cum_product)
35+
products[i] = cum_product
36+
cum_product *= int_list[i]
37+
# Product of all numbers after int
38+
after_product = int_list[-1]
39+
for j in range(2,len(int_list)+1):
40+
print("after: ", len(int_list)+1-j, after_product)
41+
products[-j] *= after_product
42+
after_product *= int_list[-j]
43+
print(products)
44+
return products
45+
46+
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+
def product(list_of_ints):
59+
pass
60+
61+
62+
def max_three(list_of_ints):
63+
pass
64+
65+
66+
def min_three(list_of_ints):
67+
pass
68+
69+
70+
def highest_product_from_list_of_ints(list_of_ints):
71+
if len(list_of_ints) == 3:
72+
return product(list_of_ints)
73+
elif min(list_of_ints) >= 0:
74+
return product(max_three(list_of_ints))
75+
elif max(list_of_ints) <= 0:
76+
return product(min_three(list_of_ints))
77+
# If some are positive and some are negative
78+
else:
79+
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+
86+
87+
class Tests(unittest.TestCase):
88+
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])
91+
92+
if __name__ == '__main__':
93+
unittest.main()

0 commit comments

Comments
 (0)