I wrote this program in python to see if a list is ascending and its not working, can someone help me?
list1 = [1, 2, 3, 4]
print (list1)
length = len(list1)
run_started = False
for x in range(length - 1):
    t = list1[x + 1] - list1[x]
    if t > 0 :
        if run_started:
            run_length = x
        else:
            run_started = True
            run_length = x
    else:
        if run_started:
            print (True)
            print ("Run Length: {0}".format(run_length))
            break
if not run_started:
    print (False)
                I'd say the easiest (although not the most efficient) way would be:
list1 = [3, 1, 2, 4]
if sorted(list1) == list1:
    print "list1 is sorted"
                        Well, here's my go at it. Some have suggested sorting the list, which would be O(nlogn). I propose a simpler O(n) solution
def isAscending(list):
    previous = list[0]
    for number in list:
        if number < previous:
            return False
        previous = number
    return True
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With