03-Function Practice Exercises
The solution that has been provided for the question "Summer of 69" doesn't give the correct solution.
When it is tested with summer_69([1, 3, 5]) => it is OK but summer_69([6,1, 3, 5]) => it is not correct.
My solution is ;
def summer_69(arr): total = 0 start = 0 end = 0 flag = False
for i in range(len(arr)):
if arr[i] == 6:
flag = True
start = i
if arr[i] == 9 and flag:
end = i
total += arr[i]
if start >= 0 and end >= 1:
total = total - sum(arr[start:end+1])
return total
i didn't understand the question could please text me the question
in the question it says that every 6 will be followed by at least one 9 so your second arrays isn't supposed to work
you need to have 9 somewhere after 6 as input.
My solution
def summer_69(traverse_list: list) -> int:
total = 0
found_six = False
for element in traverse_list:
if element == 6:
found_six = True
if not found_six:
total += element
if element == 9:
found_six = False
return total
Suraj-mandal
Thank you very much for posting your solution, it helped me a lot!
I'm confused as to how line 22 is correct. In line 21, it seems like the logic is saying that if the number 9 is not found while we are not adding, break from not adding, and continue to add. And the following two lines are saying otherwise, keep adding.
Why should we keep adding if we have discovered a 9 while we are in 'not adding' status? It looks like we entered 'not adding' status because we have previously discovered a 6.
Can someone clarify why line 21 should be add=True?
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
@LeeZee1 Why is there a code after the return statement? In fact, the code below return statement is practically not reachable at all.
My Solution for SUMMER OF '69 question.
def summer_69(arr):
if 6 in arr:
return sum(arr) - sum(arr[arr.index(6):arr.index(9) + 1])
else:
return sum(arr)
VigneshSahoo, Sorry about that. Somehow, I inadvertently added the code below the return statement. I have now edited it out.
Thank you for posting your solution. I was in fact playing with your idea of specifying the index of values 6 and 9 and then subtracting out the values in that section as well.
But, about the official posted solution I posted above, briefly put, I am not understanding why add=True in line 22 is correct. I know the code works to get the answers, but it seems counterintuitive.
Can someone please explain?
Thank you.
@LeeZee1, To understand add = true, we need to start from the beginning.
while add:
if num != 6:
total += num
break
else:
add = False
The first section of code inside 'while' loop adds the selected number to 'total' and breaks(till 6 appears). And when 6 appears, the code inside else changes the add to False. If you notice the while loop, it says 'while add' i.e., when while == True, this code will be executed and the number will be added to the 'total'.
But we need to skip the numbers from 6 to 9. So, when 6 is the number, we are simply passing the break command so that the 'for' loop can move ahead without adding the number to the 'total'.
But the problem here is, we have changed add = False when the number is 6. This means, first while loop will not work until we change the add to True again so that we can add the number after 9.
Here comes the second 'while loop' to rescue.
while not add:
if num != 9:
break
else:
add=True
break
This code of block will executive only when 'while == False', which we updated using the last line of first while loop. This loops checks for 9. If the number is not 9, it will break the while loop and go back to the 'for' loop. But now, since 'add = False', first while loop won't run(like explained above).
So, the second while loop waits for 9 to appear and when 9 is the selected number, the code inside else will change add to true. This will allow the first while loop to add the numbers after 9.
You will be able to understand the below code easily. Check it.
while not add:
if num == 9:
add = True
else:
break
So, in simple words, we can tell that, add = True let's the first while loop to add the numbers after 9 to the 'total'.
Ok, but if add is changed to True when a 9 appears as under else in 2nd while loop, why wouldn't it just add the 9 to the 6? After all, the add was changed to True upon discovering the 9, and by switching add to True, you'd think that the 9 WOULD be added.
But, I guess add is switched to True AFTER it passes over the 9 instead of the 9 getting incorporared into the adding loop.
Furthermore, when there is a break as in the first loop after a number other than 6 is discovered, that number is incorporated into the adding loop and added to the total. I suppose the order of operations is different for a break than it is for add=True.
I'll just have to remember that the add=true command operates counterinutitively like that.
@LeeZee1, I think you totally misunderstood. add = True or add = False is just a switch here. It doesn't do any operation. When add = Ture, first while loop will work and when it is false second while loop will work. Inbetween. we are just turning the switch 'to add' when 6 comes and turning it on when '9 comes.' Let's rewrite the code. This might help you.
def summer_69(arr):
total = 0
add = True
for num in arr:
while add:
if num == 6:
add = False
break
else:
total += num
break
while not add:
if num == 9:
add = True
else:
break
return total
Ok, thanks. I think I get it :)
@LeeZee1, To understand add = true, we need to start from the beginning.
while add: if num != 6: total += num break else: add = FalseThe first section of code inside 'while' loop adds the selected number to 'total' and breaks(till 6 appears). And when 6 appears, the code inside else changes the add to False. If you notice the while loop, it says 'while add' i.e., when while == True, this code will be executed and the number will be added to the 'total'.
But we need to skip the numbers from 6 to 9. So, when 6 is the number, we are simply passing the break command so that the 'for' loop can move ahead without adding the number to the 'total'.
But the problem here is, we have changed add = False when the number is 6. This means, first while loop will not work until we change the add to True again so that we can add the number after 9.
Here comes the second 'while loop' to rescue.
while not add: if num != 9: break else: add=True breakThis code of block will executive only when 'while == False', which we updated using the last line of first while loop. This loops checks for 9. If the number is not 9, it will break the while loop and go back to the 'for' loop. But now, since 'add = False', first while loop won't run(like explained above).
So, the second while loop waits for 9 to appear and when 9 is the selected number, the code inside else will change add to true. This will allow the first while loop to add the numbers after 9.
You will be able to understand the below code easily. Check it.
while not add: if num == 9: add = True else: breakSo, in simple words, we can tell that, add = True let's the first while loop to add the numbers after 9 to the 'total'.
Hi,
My query pertains to the second while loop ,
before going to the second while loop we have converted "add =False" using the else statement, now the while loop goes as
while !=add
- doesnt this mean while != to False means " while not equal to False rather than than while == False ?*