Fundamentals 7 min read

Master the ‘Sum of Numbers Ignoring 6‑9 Sections’ Challenge in Python

This article walks through a Python list‑processing problem that requires summing numbers while skipping any segment that starts with a 6 and ends with the next 9, presenting multiple solution approaches—including index tricks, flag control, while loops, and recursion—to help readers understand and implement the algorithm effectively.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master the ‘Sum of Numbers Ignoring 6‑9 Sections’ Challenge in Python

1. Introduction

The author, a Python enthusiast, shares a question from a fan named Chloe about a list‑processing problem: return the sum of numbers in an array while ignoring any section that starts with a 6 and ends with the next 9.

2. Solution Process

Several solutions are presented.

Solution by Xiao Wang

def summer69(arr):
    index_of_6 = arr.index(6)
    index_of_9 = len(arr) - arr[::-1].index(9) - 1
    sum_of_arr = 0
    for i, j in enumerate(arr):
        if index_of_6 <= i <= index_of_9:
            pass
        else:
            sum_of_arr += arr[i]
    return sum_of_arr

print(summer69([6, 6, 7, 8, 9, 8, 9, 2, 4]))

This approach finds the first 6 and the last 9 and skips the numbers in between, but it fails on the first test case.

Flag‑based solution by Moon God

def summer_69(arr):
    total = 0
    flag = 1
    for i in arr:
        if i == 6:
            flag = 0
        elif i == 9:
            flag = 1
            continue
        total = total + i * flag
    return total

While‑loop version by Moon God

def summer_69(arr):
    total = 0
    length = len(arr)
    left = 0
    while left < length:
        if arr[left] == 6:
            for i in range(left, length):
                left = i
                if arr[i] == 9:
                    break
        else:
            total += arr[left]
        left += 1
    return total

Solution by Chloe (state flag variable)

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

Recursive solution by Teacher Yu Liang

def summer_69(arr, res=0):
    if 6 in arr and 9 in arr:
        b = arr.index(6)
        e = arr.index(9)
        if b < e:
            del arr[b:e+1]
        else:
            res = res + 9
            del arr[e]
        return summer_69(arr, res)
    else:
        res += sum(arr)
        return res

Another recursive approach by Moon God

def s69(arr, total=0, status=1):
    if not arr:
        return total
    if arr[0] == 9 and status == 0:
        return s69(arr[1:], total, status=1)
    elif arr[0] == 6 or status == 0:
        return s69(arr[1:], total, status=0)
    else:
        return arr[0] + s69(arr[1:], total, status)

All methods pass the three given test cases, and the flag‑based solution is recommended for its clarity.

3. Summary

The article demonstrates five different Python implementations for the “sum‑except‑6‑to‑9” problem, covering straightforward index handling, flag control, while loops, and recursion, providing readers with multiple perspectives to solve similar list‑processing challenges.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmPythonRecursionListcoding-challenge
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.