Your code looks good. It's always nice to see people following PEP 8. There is a small issue though with how you check if a list is empty or not. Currently you write
elif not len(exclude_stack):
or elif num == 9 and len(exclude_stack):
but PEP 8 actually has the following recommendation for this kind of tests:
For sequences, (strings, lists, tuples), use the fact that empty
sequences are false.
Yes: if not seq:
if seq:
No: if len(seq):
if not len(seq):
So, in your case, it should be elif not exclude_stack:
and elif num == 9 and exclude_stack:
.
Additionally, the details on implementation in the docstring look redundant, I would remove them and leave only the task description.
The main question I have regarding your implementation is: Do you really need to have the stack to keep track if you are inside the 6-9 section? If I print exclude_stack
for this input summer_sum([6] * 10 + [9] * 10)
, it will look like this:
[0]
[0, 0]
[0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0]
[0, 0]
[0]
[]
This doesn't really look like the most optimal solution memory-wise. How about keeping a track of the 6-9 sections by simply incrementing/decrementing some integer variable instead? Something like this:
def summer_sum(a_list):
"""Solution for Summer 69 challenge"""
section_depth = 0
total = 0
for num in a_list:
if num == 6:
section_depth += 1
elif section_depth == 0:
total += num
elif num == 9 and section_depth > 0:
section_depth -= 1
return total
I assume this will be also a bit faster as you won't have to pop or append to a list.
My hands are also itching to remove the total
variable and make a generator function, but this may be a bit overboard:
def summer_sum(a_list):
"""Solution for Summer 69 challenge"""
def free_numbers(numbers):
section_depth = 0
for num in numbers:
if num == 6:
section_depth += 1
elif section_depth == 0:
yield num
elif num == 9 and section_depth > 0:
section_depth -= 1
return sum(free_numbers(a_list))
[6,9,9]
return? What does[9]
return? \$\endgroup\$ – ben rudgers Jul 31 '19 at 14:18[6, 6, 9, 5]
returns 0. The question says, "every 6 will be followed by at least one 9." \$\endgroup\$ – sg7610 Jul 31 '19 at 23:04