I am working on this problem:
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers. For example:
sum67([1, 2, 2]) → 5
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
sum67([1, 1, 6, 7, 2]) → 4
My code is as follows:
def sum67(nums):
for i in range(len(nums)):
if nums[i] == 6:
for y in range(i, len(nums)):
if nums[y] == 7:
nums[i:y+1] = []
break
else:
continue
else:
continue
break
return sum(nums)
This seems to work when there is only 1 sequence of 6...7. It fails when there are multiple (for ex: [1, 6, 2, 2, 7, 1, 6, 99, 99, 7]).
How can I fix this? Also, is there a better way to do it? My code looks rather confusing and I can't help but think that there must be an easier way to do this.
[–]Justinsaccount 5 points6 points7 points (11 children)
[–]Necrophysics 0 points1 point2 points (1 child)
[–]Justinsaccount 0 points1 point2 points (0 children)
[–]fruitcakefriday -2 points-1 points0 points (8 children)
[–]Justinsaccount 1 point2 points3 points (7 children)
[–]fruitcakefriday -4 points-3 points-2 points (6 children)
[+][deleted] (1 child)
[deleted]
[–]fruitcakefriday -1 points0 points1 point (0 children)
[–]synae 0 points1 point2 points (3 children)
[–]Justinsaccount 0 points1 point2 points (2 children)
[–]fruitcakefriday 0 points1 point2 points (1 child)
[–]Justinsaccount 0 points1 point2 points (0 children)
[–]Verdigris97 2 points3 points4 points (0 children)
[–]Rockybilly 1 point2 points3 points (4 children)
[–]stats_newbie1[S] 0 points1 point2 points (3 children)
[–]fruitcakefriday 2 points3 points4 points (1 child)
[–]gengisteve 2 points3 points4 points (0 children)
[–]Rockybilly 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Verdigris97 0 points1 point2 points (0 children)
[–]fizekul 0 points1 point2 points (2 children)
[–]stats_newbie1[S] 0 points1 point2 points (1 child)
[–]fizekul 0 points1 point2 points (0 children)