How to ignore numbers after specific number in the list/array in Python?

Question:

I’m stuck on this problem:

I was trying to return sum of list of numbers in the array ignoring sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.

Here are my test cases:

number_69([1, 3, 5]) --> 9
number_69([4, 5, 6, 7, 8, 9]) --> 24
number_69([2, 1, 6, 9, 11]) --> 14

I came up with something like:

def number_69(arr):
   for num in arr:
     if 6 not in arr and 9 not in arr:
        return sum(arr)
     return 0
Asked By: Mo Fatah

||

Answers:

Great name for a function BTW

def summer_69(arr):
    toSum = True
    sum = 0
    for x in arr:
        if toSum :
            if(x == 6):
                toSum = False
            else :
                sum += x
        else :
            if(x == 9):
                toSum = True
    return sum

Hope it is useful

Answered By: O.Cuenca

i guess we stop adding when we see 6 and we start again when we see 9

def number_69(arr):
    sum = 0
    stop = False
    for num in arr:
        if num == 6:
            stop = True
        elif num == 9:
            stop = False
        elif stop is False:
            sum = sum + num
    return sum

print(number_69([2, 1, 6, 9, 11]))
Answered By: D. Seah

You can iteratively slice out the parts between the 6 and the 9 until there are no more pairs, then call sum on the remainder.

For ‘slicing’ we use the Python’s index slicing, which works by giving a start index and an end index (which will not be included).

>>> [0, 1, 2, 3][1:3] == [1, 2]
True
>>> [0, 1, 2, 3][1:]
[1, 2, 3]
>>> [0, 1, 2, 3][:2]
[0, 1]

We find the locations of the first 6 and the first following 9 with list.index. We do have to make sure to only start looking for a 9 after the 6. This gives

def number_69(arr):
    while 6 in arr:
        index = arr.index(6)
        arr = arr[:index] + arr[index + arr[index:].index(9) + 1:]
        # +index because we are removing it from arr[index:]
        # +1 because we don't want to include the 9 in the new list
    return sum(arr)

Since we know that every 6 will be followed by a 9, there is no need to check whether there is a 9 in the list. Thus, this function will remove all 6-9 blocks and only then return the sum of the entire list.

If there is a 6 without an accompanying 9 this function will raise a ValueError. This can be solved by checking for a 9 anyways, but this has to be done after the index of the first 6. If no 9 is found, we also have to break out of the loop, since the 6 will not be removed.

def number_69(arr):
    while 6 in arr:
        index = arr.index(6)
        if 9 in arr[index:]:
            arr = arr[:index] + arr[index + arr[index:].index(9) + 1:]
            # +index because we are removing it from arr[index:]
            # +1 because we don't want to include the 9 in the new list
        else:
            break
    return sum(arr)
Answered By: Hephaistos-plus

Another attempt, using list.pop:

lst = [4, 5, 6, 7, 8, 9]

def summer_69(lst):
    s = 0
    while lst:
        i = lst.pop()
        if i == 9:
            while i!=6:
                i = lst.pop()
        else:
            s += i
    return s

print(summer_69(lst))

Prints:

9
Answered By: Andrej Kesely
def summer_69(arr):
    pop = []
    for i in arr:
        if i <=9 and i>=6:
            continue
        else:
            pop.append(i)
    return pop
Answered By: Juissi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.