Now trying to figure out how to find all the maximal subsequences(both positive and negative) in sequence. Here I got some troubles, because there is no suitable solution found. I've this code, but the output is ok only for positive numbers. I'm newbie to python so can not figure out in short time how can this be handled.
testcase = [1,2,3,4,5,4,3,2,1,0,-1,-2,-3,-2,-1,-2,-1,-2,-3,-4,-5]
def process(lst):
def sub(lst):
temp = []
while lst and (not temp or temp[-1] <= lst[0]):
temp.append(lst[0])
lst = lst[1:]
return temp, lst
res=[]
while lst:
subres, lst = sub(lst)
res.append(subres[0] if len(subres)==1 else subres)
return res
if __name__ == "__main__":
print(process(testcase))
So, the sample output is
[[1, 2, 3, 4, 5], 4, 3, 2, 1, 0, -1, -2, [-3, -2, -1], [-2, -1], -2, -3, -4, -5]]
While, I want it to be
[[1,2,3,4],[5,4,3,2,1,0,-1,-2,-3],[-2,-1],-2,[-1,-2,-3,-4,-5]]
So, the question is how can this be done?