A function that checks if the list is sorted - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: A function that checks if the list is sorted (/thread-19176.html) Pages:
1
2
|
RE: A function that checks if the list is sorted - micseydel - Jun-18-2019 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. RE: A function that checks if the list is sorted - ThomasL - Jun-18-2019 (Jun-18-2019, 01:33 AM)micseydel Wrote: The OP's solution is superior to the one-liner in two waysThe OP´s code is no solution as it doesn´t work. But i understand what you mean :-) RE: A function that checks if the list is sorted - micseydel - Jun-18-2019 The original poster did write a correct solution, unless I'm mistaken. That's what I'm referring to. RE: A function that checks if the list is sorted - ThomasL - Jun-18-2019 Typical misunderstanding, i thought you referred to the OP´s first code. RE: A function that checks if the list is sorted - MIRA - Apr-21-2024 (Jun-16-2019, 05:42 PM)noisefloor Wrote: Hi, 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 RE: A function that checks if the list is sorted - Pedroski55 - Apr-22-2024 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 RE: A function that checks if the list is sorted - DeaD_EyE - Apr-22-2024 The useful function itertools.pairwise was added to Python 3.10.https://docs.python.org/3/library/itertools.html#itertools.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") |