Sum of numbers in a Python list except 13 and numbers after it
Question:
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
def sum13(nums):
if nums == []:
return 0
sum=0
for i in range(len(nums)):
if nums[i] == 13:
break
else:
sum = sum+nums[i]
return sum
The above code is failing only at:
- sum13([1, 2, 13, 2, 1, 13]) → 4 while my answer is 3 (how 4?)
- sum13([13, 1, 2, 13, 2, 1, 13]) → 3 while my answer is 0 (again, how is 3 correct if there are no numbers before 13?)
Please explain the logic and what I’ve done wrong.
Answers:
Numbers "immediately after a 13" means that any number directly following a 13 in the array is ignored. For example, in [1, 2, 13, 2, 1, 13], the 2 after the 13 is ignored, but the 1 after that 2 counts.
The numbers that come immediately after 13, not ALL the numbers after 13.
def sum13(nums):
if nums == []:
return 0
sum=0
i = 0
while i <= len(nums): #change this to a while loop so we can skip numbers
if nums[i] == 13:
i+=1 # if it's 13 we skip an extra number
else:
sum = sum+nums[i]
i+=1 # this always increments the counter, but then on 13 we are now incrementing twice
return sum
print(sum13([1, 2, 13, 2, 1, 13]))
If you´re surfing through stacks trying to find some elegant solution, here´s mine:
def sum13(nums):
if nums == 0:
return 0
soma = 0
i = 0
while i < len(nums):
if nums[i] == 13:
i += 2
continue
else:
soma += nums[i]
i += 1
return soma
def damned_13(nums):
for i in nums:
if i == 13:
return sum(nums[:nums.index(13)])
return sum(nums)
print(damned_13([1, 2, 2, 1, 13, 55]))
or
def damned_13(nums):
if nums.count(13) == 1:
return sum(nums[:nums.index(13)])
return sum(nums)
print(damned_13([1, 2, 2, 1, 13, 55]))
a=['1','2','13','2','1','13']
b=a.index('13')
a=a[:b]
a=list(map(int,a))
print(sum(a))
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
def sum13(nums):
if nums == []:
return 0
sum=0
for i in range(len(nums)):
if nums[i] == 13:
break
else:
sum = sum+nums[i]
return sum
The above code is failing only at:
- sum13([1, 2, 13, 2, 1, 13]) → 4 while my answer is 3 (how 4?)
- sum13([13, 1, 2, 13, 2, 1, 13]) → 3 while my answer is 0 (again, how is 3 correct if there are no numbers before 13?)
Please explain the logic and what I’ve done wrong.
Numbers "immediately after a 13" means that any number directly following a 13 in the array is ignored. For example, in [1, 2, 13, 2, 1, 13], the 2 after the 13 is ignored, but the 1 after that 2 counts.
The numbers that come immediately after 13, not ALL the numbers after 13.
def sum13(nums):
if nums == []:
return 0
sum=0
i = 0
while i <= len(nums): #change this to a while loop so we can skip numbers
if nums[i] == 13:
i+=1 # if it's 13 we skip an extra number
else:
sum = sum+nums[i]
i+=1 # this always increments the counter, but then on 13 we are now incrementing twice
return sum
print(sum13([1, 2, 13, 2, 1, 13]))
If you´re surfing through stacks trying to find some elegant solution, here´s mine:
def sum13(nums):
if nums == 0:
return 0
soma = 0
i = 0
while i < len(nums):
if nums[i] == 13:
i += 2
continue
else:
soma += nums[i]
i += 1
return soma
def damned_13(nums):
for i in nums:
if i == 13:
return sum(nums[:nums.index(13)])
return sum(nums)
print(damned_13([1, 2, 2, 1, 13, 55]))
or
def damned_13(nums):
if nums.count(13) == 1:
return sum(nums[:nums.index(13)])
return sum(nums)
print(damned_13([1, 2, 2, 1, 13, 55]))
a=['1','2','13','2','1','13']
b=a.index('13')
a=a[:b]
a=list(map(int,a))
print(sum(a))