Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14271

Summing up a list of numbers with a twist

$
0
0

I want to sum up a list of numbers excluding 6 and 9 and every number in between them. The twist is the list has multiple 6s and 9s.

*Edit : numbers should only be excluded if it is between 6 and 9(including 6 and 9) and vice versa , only in this order .([6,n1,n2,n3,n4,9]or[9,n1,n2,n3,n4,n5,6])

  • numbers between two 6s [6,n1,n2,n3,6] andtwo 9s [9,n1,n2,n3,n4,9] should be included.

  • expected result : total = sum(1,2,3,4,7,2,4,4,3,2,1) .

total = 33

list1= [1,2,3,4,6,7,3,5,9,7,2,4,9,4,5,6,7,8,9,4,3,2,1]

This is the solution for the problem without the twist:

def sum6(nums):    total = 0     between6And9 = False     for i in nums:         if i == 6:             between6And9 = True        elif i == 9:             between6And9 = False        elif not between6And9:             total+= i    return total

Viewing all articles
Browse latest Browse all 14271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>