Master the ‘Summer of 69’ Python Challenge: 5 Solutions Explained
This article walks through a Python coding challenge that sums numbers in a list while ignoring sections that start with 6 and end with the next 9, presenting five distinct solutions—including flag‑based, loop‑based, and recursive approaches—complete with explanations and full code examples.
Introduction
The author, a Python enthusiast, shares a question raised by a community member about a list‑processing problem and provides a detailed walkthrough of multiple solutions.
Problem Statement
The task, often called “Summer of 69”, requires returning the sum of numbers in an array while ignoring any sections that start with a 6 and extend to the next 9. If no numbers are present, the result should be 0.
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]) --> 14Solution 1 – 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, skips the numbers between them, and sums the rest. It fails on the first test case because it assumes both 6 and 9 are present.
Solution 2 – Yue Shen (Flag Method)
def summer_69(arr):
total = 0
flag = 1 # allow accumulation
for i in arr:
if i == 6:
flag = 0
elif i == 9:
flag = 1
continue
total = total + i * flag
return totalThe flag variable indicates whether numbers should be added to the total.
Solution 3 – Yue Shen (While Loop)
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 totalThis method iterates with an index, skipping sections between 6 and the following 9.
Solution 4 – Chloe (Alternative)
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 totalThis version toggles an “add” flag using nested while loops.
Solution 5 – Yu Liang (Recursive)
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
print(summer_69([1, 3, 5]))
print(summer_69([4, 5, 6, 7, 8, 9]))
print(summer_69([2, 1, 6, 9, 11]))The recursive approach removes the 6‑to‑9 segment (or handles the edge case where 9 appears before 6) and calls itself until no such segment remains.
Conclusion
The article presents five different implementations for the “Summer of 69” problem, ranging from straightforward index calculations to flag‑based loops and recursive strategies, allowing readers to compare styles and choose the one that best fits their understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
