solutions

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 (18 - 7 - 3)
subtract_reversed([9]) # Result: 9 (9 - 0....)

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

Test Cases

test list with many elements - Run Test

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

test empty list - Run Test

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

test with one element - Run Test

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

test list with two elements - Run Test

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