Last active
June 8, 2018 13:39
-
-
Save MSeifert04/27c52811540335dca993e2e1bdd9b1a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from iteration_utilities import all_monotone | |
from itertools import tee | |
import operator | |
def is_sorted_by_alexandre(iterable, compare=operator.le): | |
a, b = tee(iterable) | |
next(b, None) | |
return all(map(compare, a, b)) | |
def is_sorted_using_sorted(iterable): | |
return sorted(iterable) == iterable | |
def is_sorted_by_timgeb(lst): | |
return all(x <= y for x,y in zip(lst, lst[1:])) | |
def is_sorted_by_sergey(l): | |
l2 = iter(l) | |
next(l2, None) | |
return all(a <= b for a, b in zip(l, l2)) | |
def is_sorted_by_wai(l): | |
all(l[i] <= l[i+1] for i in range(len(l)-1)) | |
def is_sorted_by_paul(l): | |
return all(a <= b for a, b in zip(l[:-1], l[1:])) | |
def is_sorted_by_aaronasterling(lst, key=lambda x, y: x < y): | |
for i, el in enumerate(lst[1:], 1): | |
if key(el, lst[i-1]): | |
return False | |
return True | |
def is_sorted_by_AmitMoscovich(lst): | |
it = iter(lst) | |
try: | |
prev = next(it) | |
except StopIteration: | |
return True | |
for x in it: | |
if prev > x: | |
return False | |
prev = x | |
return True | |
# sorted list | |
l = list(range(10000)) | |
%timeit all_monotone(l) | |
%timeit is_sorted_using_sorted(l) | |
%timeit is_sorted_by_AmitMoscovich(l) | |
%timeit is_sorted_by_alexandre(l) | |
%timeit is_sorted_by_timgeb(l) | |
%timeit is_sorted_by_paul(l) | |
%timeit is_sorted_by_sergey(l) | |
%timeit is_sorted_by_wai(l) | |
%timeit is_sorted_by_aaronasterling(l) | |
# unsorted list | |
l = [10] + list(range(10000)) | |
%timeit all_monotone(l) | |
%timeit is_sorted_using_sorted(l) | |
%timeit is_sorted_by_AmitMoscovich(l) | |
%timeit is_sorted_by_alexandre(l) | |
%timeit is_sorted_by_timgeb(l) | |
%timeit is_sorted_by_paul(l) | |
%timeit is_sorted_by_sergey(l) | |
%timeit is_sorted_by_wai(l) | |
%timeit is_sorted_by_aaronasterling(l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment