Tuesday, 20 August 2013

To stop finding running average once the condition fails?

To stop finding running average once the condition fails?

Consider the code below:
sub = [767220, 769287, 770167, 770276, 770791, 770835, 771926, 1196500,
1199789, 1201485, 1206331, 1206467, 1210929, 1213184, 1213204,
1213221, 1361867, 1361921, 1361949, 1364886, 1367224, 1368005, 1368456,
1368982, 1369000, 1370365, 1370434, 1370551, 1371492, 1471407, 1709408,
1710264, 1710308, 1710322, 1710350, 1710365, 1710375]
avg = []; final = []
def runningMean(seq, n=0, total=0): #fuction called recursively
if not seq:
return []
total =total+int(seq[-1])
return runningMean(seq[:-1], n=n+1, total=total) + [total/float(n+1)]
def main():
avg = runningMean(sub,n = 0,total = 0) #fuction call to obtain running
mean starting from last element in the list i,e 1710375
print avg
for i in range(len(sub)):
if (int(sub[i]) > float(avg[i] * 0.9)): #cheking the condition
final.append(sub[i])
print final
if __name__ == '__main__':
main()
output consists of list of runningmean & the sub list doesn't statisfy the
condition :
[1282960.6216216215, 1297286.75, 1312372.4571428571, 1328319.6764705882,
1345230.0909090908, 1363181.3125, 1382289.2580645161, 1402634.7,
1409742.7931034483, 1417241.142857143, 1425232.111111111,
1433651.3846153845, 1442738.76, 1452397.5, 1462798.0869565217,
1474143.2727272727, 1486568.142857143, 1492803.2, 1499691.7368421052,
1507344.111111111, 1515724.0, 1525005.25, 1535471.9333333333,
1547401.642857143, 1561126.2307692308, 1577136.75, 1595934.1818181819,
1618484.2, 1646032.3333333333, 1680349.875, 1710198.857142857,
1710330.6666666667, 1710344.0, 1710353.0, 1710363.3333333333, 1710370.0,
1710375.0]
[1361867, 1361921, 1361949, 1364886, 1367224, 1709408, 1710264, 1710308,
1710322, 1710350, 1710365, 1710375]
What i need to do is it should stop the finding of running average once
the condition fails (sub[i] > float(avg[i] * 0.9)) i,e the result should
be
[1680349.875, 1710198.857142857, 1710330.6666666667, 1710344.0,
1710353.0, 1710363.3333333333, 1710370.0, 1710375.0]
[1709408, 1710264, 1710308, 1710322, 1710350, 1710365, 1710375]
If anyone could suggest a solution in python for this it will be
helpful..........

No comments:

Post a Comment