-1

I only started coding about a month ago and I am currently having a problem with this question:

"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."

My attempt is the following:

def sum67(nums):

    summe=0
    
    if 6 not in nums:
        for elem in nums:
            summe+= elem
    else:
      indexof_6= nums.index(6)
      indexof_7= nums.index(7)
      copynums= nums[:]
      del(copynums[(indexof_6) : (indexof_7+1)])
      for i in copynums: 
          summe+= i
    return summe

unfortunately it only returns correct values for lists like [2,3,4,6,7,9] where only one 6 is present. Whereas it returns wrong values when there is more than one 6 e.g. [5,6,10,7, 5,6,9,7,1].

I already tried looking for different solutions online but i don't seem to understand them ... is my attempt with ".index()" wrong? can this one only catch the first index of a "6" ?

I would be extremely grateful for a few hints on how to correct myself :) thanks so much in advance! kind regards

Aplet123
  • 33,825
  • 1
  • 29
  • 55
Ginny
  • 1
  • 1
  • 1
    Does this answer your question? [How to find all occurrences of an element in a list](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – Mr. T Dec 20 '20 at 17:34
  • Check the documentation for `index()`. It takes an optional parameter that determins where to start thte search. – Code-Apprentice Dec 20 '20 at 17:36

3 Answers3

1

Yes, index only returns the index of the first match. One option is to keep deleting elements while a 6 exists then to sum them at the end:

def sum67(nums):
    while 6 in nums:
        del nums[nums.index(6):nums.index(7) + 1]
    return sum(nums)

Note that this is a bit inefficient and can be sped up by just looping through and maintaining a boolean flag:

def sum67(nums):
    should_add = True
    total = 0
    for num in nums:
        if num == 6:
            should_add = False
        if should_add:
            total += num
        elif num == 7:
            should_add = True
    return total
Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Thanks so much !! this was super helpful :) I got it up and working with your first suggestion ( by integrating the while-loop ) and will gladly try to understand your second suggestion as well! Have a great day:) – Ginny Dec 20 '20 at 17:45
0

On every iteration you should keep track wether you are in the 6-7 section or not and sum based on that condition. Let's take a boolean in_section variable for example:

def sum67(nums):
    summe = 0
    in_section = False
    for elem in nums:
        if elem == 6:
            in_section = True
        if not in_section:
            summe += elem
        if elem == 7:
            in_section = False
    return summe

Note that if elem is a 7 it will be not part of the sum because in_section is disabled after we summed. Mind the order of activating the section order if you need to!

Rihak
  • 13
  • 2
  • Thank you very much for your answer! I read it trough a few times and understood it now. Your comment about the order & when "in_section" is disabled was very useful and well explained, so even a beginner like me was able to get it :)! Cheers! – Ginny Dec 20 '20 at 18:00
0

You should take into account that a 7 may occur before a 6, so your calls of index could give you the wrong position of the 7 that you really need. For instance, if the input is [6,7,7,6,7], then the middle 7 is not a delimiter, and should be summed up.

I would avoid using del:

def sum67(nums):
    summe = sum(nums)
    after_seven = 0
    while True:
        try:
            six = nums.index(6, after_seven)
            after_seven = nums.index(7, six) + 1
            summe -= sum(nums[six:after_seven])
        except ValueError:
            break
    return summe
trincot
  • 317,000
  • 35
  • 244
  • 286