Solution 1

``````def subtract_reversed(a_list):
if len(a_list) == 0:
return 0

result = a_list[-1]
index = 2
while index <= len(a_list):
elem = a_list[-1 * index]
result -= elem
index += 1

return result
``````

# Subtract Reversed

Using a while loop, write a function `subtract_reversed` that receives a list and subtracts all the numbers, starting from the end. Example:

``````subtract_reversed([3, 7, 18]) # Result: 8 (which is equals to: 18 - 7 - 3)
subtract_reversed([9]) # Result: 9 (which is equals to: 9 - 0)
``````

Special case: If the list is empty, should return 0. Check the tests for details.

### Test Cases

test list with many elements -

``````def test_list_with_many_elements():
assert subtract_reversed([2, 7, 9, 32]) == (32 - 9 - 7 - 2)
``````

test empty list -

``````def test_empty_list():
assert subtract_reversed([]) == 0
``````

test with one element -

``````def test_with_one_element():
assert subtract_reversed([11]) == 11
``````

test list with two elements -

``````def test_list_with_two_elements():
assert subtract_reversed([11, 82]) == (82 - 11)
``````
def subtract_reversed(a_list): pass