Python Forum
A function that checks if the list is sorted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A function that checks if the list is sorted
#16
Of course you must compare things which are comparable.

from string import ascii_lowercase
from random import choice, randint

mylist1 = [choice(ascii_lowercase) for i in range(5)]
mylist2 = [i for i in ascii_lowercase]
mylist3 = [randint(1,10) for i in range(10)]
mylist4 = [i for i in range(10)]

def is_sorted(alist):
    for i in range(1, len(alist)-1):
        # exit as soon as the condition is not true
        if not alist[i-1] <= alist[i] <= alist[i+1]:
            return False
    return True
Reply


Messages In This Thread
RE: A function that checks if the list is sorted - by Pedroski55 - Apr-22-2024, 05:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,826 Feb-10-2021, 12:04 PM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,074 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  help! function that checks if a file is a bed file, tips so i can code it myself lilyS 1 2,400 May-31-2019, 12:41 PM
Last Post: ichabod801
  defining a function to see if a list is sorted Siylo 14 7,105 Nov-29-2018, 05:25 PM
Last Post: wavic
  sorted list not displaying Crackity 6 5,130 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020