How to search for the index of the number from the last of list to first?

  python-3.x

I was checking the documentation to find the index of the number from last to first.
I could use – list.index(number, start, end). In my example, if I omit the start and end I don’t get any exception. However, I get an exception when I change the start to the last index and end to the first element index i.e. 0. I am sure I am doing something wrong but I am unable to think about what is going wrong here.

def nonConstructibleChange(coins):
    # o(log n) - sorting
    coins.sort() # O(log n)
    max_amount = sum(coins)
    print(max_amount)
    # range is [start, end)
    if len(coins) == 0:
        return 1

    for change in range(1, max_amount+1):
        if change == 1:
            if 1 not in coins:
                return 1 
        else:
            max_limit = change - 1        
            while max_limit not in coins and max_limit != -1:
                max_limit -=1
            print(coins.index(max_limit, len(coins)-1, 0))  # throws exception
            print(coins.index(max_limit)) # this works
            
         
        
if __name__ == '__main__':
   coins = [5, 7, 1, 1, 2, 3, 22]
   nonConstructibleChange(coins)

Source: Python-3x Questions

LEAVE A COMMENT