Assert statement doesn't work properly in bigger or smaller condition

49 views Asked by At


I wrote an Algorithm that moves specific elements to specific index of array.

arr = [1,0,4,0,True,None,-12,"Jacob"]

def Move(arr,target,index):
    assert index>len(arr) , "index out of array range"
    assert index<0 , "index can't be less than zero"
    _list = []
    for val in arr:
        if val==target:
            _list.append(target)
            arr.pop(arr.index(val))
    [ arr.insert(index,val) for val in _list ]
    return arr
    
Move(arr,0,4)

Everything works perfect but I faced a problem in assert statement that shows (index>len(arr)) condition doesn't work properly in any index.

Error:>
        assert index>len(arr) , "index out of array range"
    AssertionError: index out of array range

Every time I get this when logically I shouldn't get this at sub-zero indexes

What's wrong with that?

0

There are 0 answers