Imagine I have
[1,2,[2,3],[[1,2],3]]
as a string
I need to calculate the total average like this:
0.25*(1+2+(0.5*(2+3))+(0.5*(0.5*(1+2)+3))) How could you detect all the parenthesis and do the average accordingly? The biggest problem is that initially is a string so first need to convert it somehow to values.
I have this:
def total(values):
return sum(v if isinstance(v, int) else (1/len(v)) * total(v) for v in values)
But just works in case I have an array not a string. I cannot use a library