I have a script that searches for the number 13 in a given list. If it finds a 13, it needs to be removed, but I also need to remove the index after it. I got it to find all the 13s and remove them, but I'm not sure of the best way to remove the index after it. I do know I need to remove the index after 13 first because of the shift after removal. Finally I need to return the sum of all the indices, which I have done.
This is a challenge Im doing in Codebat.com, it doesn't allow for importing.
Here is what I have:
def sum13(nums):
#Return 0 for empty lists
if nums == '':
return 0
#Find 13s, remove next index and then index of 13
while(True):
if 13 in nums:
#remove the index after 13 here
nums.remove(13)
else:
break
#Return the sum of all indices.
else:
return sum(nums)
The input is random lists of ints, for example:
sum13([13, 1, 2, 13, 2, 1, 13]) # Expected Outcome: 3
sum13([1, 2, 13, 2, 1, 13]) # Expected Outcome: 4
To get the next index, I've tried variations of nums.index(i+1) in for loops, which only produces errors, and have been experimenting with list comprehensions and enumerations, but I'm not 100% on those yet.