Is there a way to check if a list is sorted either in ascending or descending order in one pass?
From this answer, I modified to also support descending order check, so my code now looks like this, is there a way to check this without having to use two for loops?
def is_sorted(check):
current = check
forwarded = check[1:]
ascending_check = all(i<=j for i, j in zip(current, forwarded))
descending_check = all(i>=j for i, j in zip(current, forwarded))
return ascending_check or descending_check
numbers = list(range(1, 10))
print(is_sorted(numbers))
reverse_numbers = list(range(10, 0, -1))
print(is_sorted(reverse_numbers))
not_sorted = [1, 3, 2, 4, 5]
print(is_sorted(not_sorted))