-1

I'm trying to write a simple code which takes a list (nums) and sums the numbers in the list as long as the number isn't 13 or doesn't occur immediately after 13. Here's my code:

def sum13(nums):
   sum = 0
   if nums[0] != 13:
      sum += nums[0]
   for i in range(len(nums)):
      if nums[i] != 13 and nums[i - 1] != 13:
         sum += nums[i]
   return sum

I keep getting a compile error: 'list index out of range'. I'm not changing the indices of the list and when I call nums[0], it's outside of the for loop which constrained i to be between 0 and len(nums), so I'm not sure how to fix this, or how to write the code in a more elegant way which eliminates the issue.

tkausl
  • 13,686
  • 2
  • 33
  • 50
  • 1
    Is the list empty? E: are you sure the error doesn't show on this line: `if nums[i] != 13 and nums[i - 1] != 13:`? – tkausl Oct 28 '17 at 01:36
  • 2
    That's a runtime error, not a compile error, by the way – OneCricketeer Oct 28 '17 at 01:38
  • You need to include the code that calls this function, I suspect that will be the source of the error. – shuttle87 Oct 28 '17 at 01:39
  • You also need to provide samples of inputs and outputs – OneCricketeer Oct 28 '17 at 01:40
  • If the nums is empty, it would raise the error. BTW, I think a "C style for statement" could be better in this case, because the iterate variable can be modified in the loop, and you can skip next element if current element is 13. There's no such `for statement` in Python, fortunately, you can use a `while` to do that. – a.l. Oct 28 '17 at 01:44
  • @aLeX, what do you mean by "C style for statement"? I'm fairly sure that this type of behavior is possible in Python, but I'd like you to clarfiy what you are meaning there – shuttle87 Oct 28 '17 at 01:45
  • @shuttle87, `for (i = 0; i < N; i++)`, In python, `i = 0; while i < N:\n #do your work\n i++` – a.l. Oct 28 '17 at 01:50
  • @aLeX, does `for i in range(N)` not do what you want? – shuttle87 Oct 28 '17 at 01:51
  • @shuttle87, there's something different: in a `for i in range(N)` statement, you can't modify the `i` in the loop. e.g. `for i in range(10):\n print(i)\n i=100` would always print 1 to 10. I've posted an answer below, maybe it could be more concise. – a.l. Oct 28 '17 at 02:14

2 Answers2

0

Interesting... I tried your code seems fine, but it will not behave as you want though...

I tried this:

def sum13(nums):
   sum = 0
   if nums[0] != 13:
      sum += nums[0]
   for i in range(len(nums)):
      print 'Current number:', str(nums[i])
      print 'Previous number:', str(nums[i-1])
      if nums[i] != 13 and nums[i - 1] != 13:
         sum += nums[i]
   return sum

ls = [10,11,13,12,14,14]
print 'Total sum:', str(sum13(ls))

returned 59, it was supposed to be (10+11+14+14) = 49.

Why this occurred? It's because you check in the loop nums[i-1].

Since i = 0 in the first loop, i-1 = -1. So, nums[-1] is the last item in your list.

Can you have a try and test this small test?

One way to fix this issue is:

def sum13(nums):
   sum = 0
   if nums[0] != 13:
      sum += nums[0]
   for i in range(1, len(nums)):
      print 'Current number:', str(nums[i])
      print 'Previous number:', str(nums[i-1])
      if nums[i] != 13 and nums[i - 1] != 13:
         sum += nums[i]
   return sum

ls = [10,11,13,12,14,14]
print 'Total sum:', str(sum13(ls))

If it still not working can you share your code and your test?

As mentioned in the comments:

def sum13(nums):
   if nums:
      sum = 0
      if nums[0] != 13:
         sum += nums[0]
      for i in range(1, len(nums)):
         print 'Current number:', str(nums[i])
         print 'Previous number:', str(nums[i-1])
         if nums[i] != 13 and nums[i - 1] != 13:
            sum += nums[i]
      return sum
   if not nums:
      print 'Empty list'
ls = [10,11,13,12,14,14]
print 'Total sum:', str(sum13(ls))
ls2 = []
print sum13(ls2)

Cheers,

kaihami
  • 815
  • 7
  • 18
0

if nums is [], visiting nums[0] would raise a IndexError.

BTW: if using a while-loop, you would got a simpler solution, see below:

def sum13(nums):
    sum = 0
    i = 0
    while i < len(nums):
        x = nums[i]
        if x == 13:
            # skip current element and next element
            i += 2
        else:
            sum += x
            i += 1
    return sum
a.l.
  • 1,085
  • 12
  • 29