2020年12月25日星期五

function to find the cumulative sum of every 5 elements.in python

There can be some elements which might not be of int type, the function will need to gracefully handle the exceptions and pass to the next element. Function is supposed to return a list where every element is expected to be the cumulative sum of every 5 elements in input list.

sample_input = [50, 30, 20, 0, "catch_me", 0, "you_got_me", "did_you_really_catch", 40, 50, "20", 0, 0, 0, 10]  expected_output = [100, 190, 200]  

I have tried so far below snip of code

a=[]  count=0  n=5  for i in sample_input:        if isinstance(i,int):          for j in range(len(sample_input)/5):              count+=i              a.append(count)  print (a)  

can some one please suggest me to how to get expected output for this problem

https://stackoverflow.com/questions/65453229/function-to-find-the-cumulative-sum-of-every-5-elements-in-python December 26, 2020 at 10:25AM

没有评论:

发表评论