I'm trying to find the average length a list stays positive or negative.
For example, I have a list
new_list = [1,2,3,4,-1,-2,-3,1,2,-4,-5,-6,-8]
I want to return the average time the list is positive and negative
so positive would be 4 + 2 = (6 / 2) = 3
and negative would be 3 + 4 = (7 / 2) = 3.5
def maxPositiveRun(lst):
count = 0
maxVal = 0
score_array = []
for n in lst:
if n > 0:
count +=1
if count > maxVal:
maxVal = count
score_array.append(maxVal)
else:
count = 0
return score_array
print(maxPositiveRun(a))
I have this code, but the problem is it adds another number when count is greater than the max value so if the list looks like this
a = [23.45,-56.34,89.56,45.23,-89.65,-56.43, 5.67,4.56,45.67,56.78]
it's going to add 3 then 4. So I am getting [1,2,3,4]
when it should really just be [1,2,4]
.
How do I return score_array
which is an array consisting of maximumal lengths of all sub-arrays which consists only of consecutive positive elements ?