How would you guys optimize this code, im getting runtime error:
def countHighlyProfitableMonths(stockPrices, k):
profitable_groups = 0
profitable_group_boolean = 1
groups = [stockPrices[i:i+k] for i in range(len(stockPrices)-k+1)]
for group in groups:
for i in range(k-1):
profitable_group_boolean *= 1 if group[i+1]>group[i ] else 0
profitable_groups += profitable_group_boolean
profitable_group_boolean = 1
return profitable_groups
def countHighlyProfitableMonths(stockPrices, k):
profitable_groups = 0
profitable_group_boolean = 1
groups = [stockPrices[i:i+k] for i in range(len(stockPrices)-k+1)]
for group in groups:
for i in range(k-1):
profitable_group_boolean *= 1 if group[i+1]>group[i ] else 0
profitable_groups += profitable_group_boolean
profitable_group_boolean = 1
return profitable_groups
Last edited: