I\'m new to programming and I\'m trying to do the codingbat.com problems to start. I came across this problem:
Given an array calculate the sum except when there is
One tricky thing to notice is something like this: [1, 13, 13, 2, 3]
You need to skip 2
too
def getSum(l):
sum = 0
skip = False
for i in l:
if i == 13:
skip = True
continue
if skip:
skip = False
continue
sum += i
return sum
Explanation:
You go through the items in the list one by one
Each time you
skip
as True
, so that you can also skip next item.skip
is True
, if it is, which means it's a item right after 13, so you need to skip this one too, and you also need to set skip
back to False
so that you don't skip next item.sum