In Python, Test If Given List Is Sorted Or Not

def isSorted(stack):
    value = True
    prv = None
    if 0 < len(stack):
        prv = stack[0]
        for cur in stack:
            if prv < cur:
                value = False
                break
    return value

if '__main__' == __name__:
    stack = [20, 20, 17, 11, 8, 8, 3, 2]
    print(isSorted(stack), stack)
    stack = [22]
    print(isSorted(stack), stack)
    stack = []
    print(isSorted(stack), stack)
    stack = [21, 22, 20]
    print(isSorted(stack), stack)
Download

Comments

Popular posts from this blog