'Return the sum of the numbers in the array,except ignore sections of numbers

SUMMER OF '69: Return the sum of the numbers in the array, except ignore 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.

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

def summer_69(arr):
    if 6 in arr and 9 in arr:
        return sum(arr[:arr.index(6)])+sum(arr[arr.index(9):])
    else:
        total=0
        for x in arr:
            total+=x
    return total

I expect the output of summer_69([4, 5, 6, 7, 8, 9]) --> 9,but the actual output is 18.
I expect the output of summer_69([2, 1, 6, 9,11]) --> 14,but the actual output is 23



Solution 1:[1]

Not sure if this is a good approach because of the string conversion.

import re

def summer_69(arr):
    if 6 in arr and 9 in arr:
        #Using Regex to remove everything between 6 & 9 and convert object back to list of ints
        arr = [int(i) for i in re.sub(r"(6.*?9)", "", "_".join(map(str, arr))).split("_") if i]  
        return sum(arr)
    else:
        return sum(arr)    

print(summer_69([1, 3, 5])) #--> 9 
print(summer_69([4, 5, 6, 7, 8, 9])) #--> 9 
print(summer_69([2, 1, 6, 9, 11])) #--> 14

Solution 2:[2]

you can do it by deleting all items between 6 and 9 (for all intervals) and just sum up the elements in the list that remained.

to remove parts between 6 and 9 you can use slice() Function

The slice() function returns a slice object.

A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end.

def summer_69(arr):
    while 6 in arr: # while 6 in array
        idx6 = arr.index(6)
        idx9 = arr.index(9)
        del arr[idx6:idx9+1] # delete part of array between index of 6 and index of 9
        print (arr) # [4, 5, 1, 1, 4, 3]
    return sum(arr)


print (summer_69([4, 5, 6, 7, 8, 9,1,1,6,2,3,9,4,3]))

output:

18

ex.

print (summer_69([1, 3, 5])) # 9
print (summer_69([4, 5, 6, 7, 8, 9])) # 9
print (summer_69([2, 1, 6, 9, 11])) # 14

Solution 3:[3]

I am a newby to programming, my solution for that problem, which considers "all" possible variations, is given below:

def summer_69(*arr):
    a=arr
    sum_a=0
    sum_a6=0
    sum_a69=0
    sum_a96=0
    sum_a9=0
    sum_each=[]
    print("given array: {}" .format(a))

    if 6 not in a:
        sum_a=sum((a))
        sum_each.append(sum_a)
        print("\nif 6 not in a:{} " .format(a))
        print("sum_a={}" .format(sum_a))
    else:
        while len(a)>0:
            if 6 in a and 9 in a:
                if a.index(6)<a.index(9):
                    a69=a[:a.index(6)]
                    sum_a69=sum((a69))
                    a69_pri=a[a.index(9):]
                    a=a69_pri
                    sum_each.append(sum_a69)
                    print("\nif a.index(6)<a.index(9): {}" .format(a69))
                    print("sum_a69={}" .format(sum_a69))
                    continue
                elif a.index(9)<a.index(6):
                    a96=a[a.index(9)+1:a.index(6)]
                    sum_a96=sum((a96))
                    a96_pri=a[a.index(6):]
                    a=a96_pri
                    sum_each.append(sum_a96)
                    print("\nif a.index(9)<a.index(6): {}" .format(a96))
                    print("sum_a96={}" .format(sum_a96))
                    continue
            elif 6 in a and 9 not in a:
                a6=a[:a.index(6)]
                sum_a6=sum((a6))
                a=a6
                sum_each.append(sum_a6)
                print("\nelif 6 in a and 9 not in a: {}" .format(a6))
                print("sum_a6={}" .format(sum_a6))
                break
            elif 6 not in a and 9 in a:
                a9=a[a.index(9)+1:]
                sum_a=sum((a9))
                a=a9
                sum_each.append(sum_a)
                print("\nelif 6 not in a and 9 in a: {}" .format(a9))
                print("sum_a9={}" .format(sum_a9))
                break

    sum_all=sum((sum_each))
    print(sum_each)
    print("sum_all={}" .format(sum_all))
    return sum_all

Solution 4:[4]

Check out my simple and straight forward running code

def summer_69(arr):
    result=0
    if 6  not in arr:
        result=result+sum(arr)
        return result
    elif len(arr)==0:
        return 0
    else:
        return sum(arr)-sum(arr[arr.index(6):arr.index(9)+1])

Solution 5:[5]

def summer_69(arr):
    total = 0
    add = True
    for num in arr:
        while add:
            if num != 6:
                total += num
                break
            else:
                add = False
        while not add:
            if num != 9:
                break
            else:
                add = True
                break
    return total

Solution 6:[6]

def summer_69(arr):
    if 6 and 9 in arr:
        c=sum(arr[arr.index(6):arr.index(9)+1)
        return sum(arr)-c
    else:
        return sum(arr)
print(summer_69([4,5,6,7,8,9]))

Solution 7:[7]

String slicing(start:stop:step) includes starting number and executes till stop. It will not include the last number. So 9 at start is included in the string and is causing the problem.

    def summer_69(arr):
         if len(arr)==0: #Return 0 if string is empty
            return 0
         elif 6 in arr: #Check for 6 is in the string
         #remove the section from 6 to 9 and return its sum
             add=arr[:arr.index(6)]+arr[arr.index(9)+1:]
             return sum(add)
         else:
             return sum(arr)

Solution 8:[8]

Your code is great but it's also taking '9' into account. To avoid that you can just do a bit of modification to your code by taking an extra +1 index position for integer 9.

def summer_69(arr):
    if 6 in arr and 9 in arr:
        return sum(arr[:arr.index(6)])+sum(arr[arr.index(9) + 1:])
    else:
        total=0
        for x in arr:
            total+=x
    return total

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rakesh
Solution 2
Solution 3
Solution 4
Solution 5 Rishabh Kumar
Solution 6 4b0
Solution 7 Pratima
Solution 8 Leau