given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
j = len(given_list) - 1
while given_list[j] < 0:
total = given_list[j]
j -= 1
print(total)
I don’t get the whole subtraction thing like j = len(given_list) – 1
and j –= 1
. I’ve memorized it, but I still don’t quite get it. Can you explain this code line by line?
CodePudding user response:
In the above snippet:
The list given_list
contains numbers sorted in descending orders.
total variable is initialised to zero and variable j is initialised to length of the list-1, that is the index of last element in the list.
The while loop iterates from the end of the list checks whether the item in the list indexed by j
variable is lesser than 0.
If list item is greater than or equal to zero then the loop terminates.
Inside the while loop the total is updated by adding the list element indexed by j variable i.e. total = given_list[j]
and same is equivalent to total=total given_list[j]
.
Then j is decremented by 1 in in j -= 1
.
Once the loop terminates then the variable total
is printed.
CodePudding user response:
The algorithm seems to call for a way to separate out and sum together the last items from the list and stop when it finds one which is larger than zero, so the design seems rather natural and obvious.
More generally, there is no specific reason for the loop to run from the end to the beginning, but this is not uncommon especially in languages where comparing something to zero is a cheaper operation than checking whether you have reached the end. You could equivalently loop over the beginning of the list and skip items until you find one which is less than zero, and then start adding them up. For small lists, like in your example, the difference should be negligible anyway. If you have really large lists, perhaps a better design altogether would be to sort them in ascending order instead.
As such, this could easily be written more Pythonically as
total = 0
for j in range(len(given_list)):
if given_list[j] < 0:
total = given_list[j]
which of course could further be simplified to
total = 0
for v in given_list:
if v < 0:
total = v
or simply
total = sum(x if x < 0 else 0 for x in given_list)
or correspondingly turned around to loop backwards like in the original with range(len(j), 0, -1)
and then you can break
out of the loop when you find a value which is not negative, assuming (like your code does) that the list is sorted in descending order.
CodePudding user response:
The following might make the goal a bit more obvious:
given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
calc_copy = given_list.copy()
total = 0
# while len(calc_copy) > 0:
while True:
cur = calc_copy.pop()
if (cur >= 0):
break
total = cur
del calc_copy
print(total)
Ref.: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists