Max Profit with K Transactions in Python
Run this code here to see live: https://repl.it/@VinitKhandelwal/max-profit-calls
Output
continuous rising - 2 calls
(5, 90, 85)
one dip - 2 calls
(30, 90, 60)
(5, 50, 45)
continuous rising - 1 call
(5, 90, 85)
one dip - one call
(5, 50, 45)
Run this code here to see live: https://repl.it/@VinitKhandelwal/max-profit-calls
class Stock:
def __init__(self, slist, count):
self.slist = slist
self.count = count
print(self._func(slist))
def _func(self, passlist):
i = passlist[0]
start = i
for index, j in enumerate(passlist[1:], 1):
if j < i:
end = i
self.count -= 1
# repeat for the number of calls to be generated
if self.count!=0:
print(self._func(passlist[index:]))
return (start, end, end-start)
i=j
end = i
return (start, end, end-start)
# passing each day's closing price of stock in list AND number of calls to be generated.
print("continuous rising - 2 calls")
obj = Stock([5,7,11,50,60,90], 2)
print("one dip - 2 calls")
obj1 = Stock([5,7,11,50,30,60,90], 2)
print("continuous rising - 1 call")
obj2 = Stock([5,7,11,50,60,90], 1)
print("one dip - one call")
obj1 = Stock([5,7,11,50,30,60,90], 1)
continuous rising - 2 calls
(5, 90, 85)
one dip - 2 calls
(30, 90, 60)
(5, 50, 45)
continuous rising - 1 call
(5, 90, 85)
one dip - one call
(5, 50, 45)
Comments
Post a Comment