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
#11
The OP's solution is superior to the one-liner in two ways:
The big-O of the OP's solution is O(n), whereas the version using sorted() is O(n**2) (or whatever Tim-sort is; definitely not linear)
The OP's code can return immediately upon finding that the list isn't sorted, it doesn't even have to do n work.

As others have mentioned enumerate(), I tend to use it instead of range(len()) just to communicate to the reader that I really do need the index. That said, I would use itertools' islice (and izip if Python 2) and avoid indexes entirely, the only time I find myself actually needing indexes is for some interview questions.
Reply
#12
(Jun-18-2019, 01:33 AM)micseydel Wrote: The OP's solution is superior to the one-liner in two ways
The OP´s code is no solution as it doesn´t work.
But i understand what you mean :-)
Reply
#13
The original poster did write a correct solution, unless I'm mistaken. That's what I'm referring to.
Reply
#14
Typical misunderstanding, i thought you referred to the OP´s first code.
Reply
#15
(Jun-16-2019, 05:42 PM)noisefloor Wrote: Hi,

@ppoyan89: iterating over an iterable with for x in range(len(iterable)) is still a bad anti-pattern. Don't you have any motivation to use what you learned before?

Except this, this is solvable without iteration but by the build-in function sorted:

def is_sorted(iterable):
    return iterable == sorted(iterable)
Regards, noisefloor


hi,
your code is only when you need to sort a list, and the question is basically asking you to see if the list is sorted return true otherwise false which doesn't work with your code
Reply
#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
#17
The useful function itertools.pairwise was added to Python 3.10.

https://docs.python.org/3/library/iterto...s.pairwise

This isn't Pythonic: range(len(something))


from itertools import pairwise
from string import ascii_lowercase
from random import choice, randint


test_data = [
    [choice(ascii_lowercase) for i in range(5)],
    [i for i in ascii_lowercase],
    [randint(1, 10) for i in range(10)],
    [i for i in range(10)],
]


def is_sorted(iterable):
    for last, current in pairwise(iterable):
        if last > current:
            return False
    return True


# test
for data in test_data:
    print(f"'{'-'.join(map(str, data))}' is {'' if is_sorted(data) else 'not'} sorted")
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,682 Feb-10-2021, 12:04 PM
Last Post: buran
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,036 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,365 May-31-2019, 12:41 PM
Last Post: ichabod801
  defining a function to see if a list is sorted Siylo 14 6,953 Nov-29-2018, 05:25 PM
Last Post: wavic
  sorted list not displaying Crackity 6 5,059 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