Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
4 one-liners with speed test solution in Speedy category for Ascending List by CDG.Axel
from typing import Iterable
from time import time
def is_ascending(items: Iterable[int]) -> bool:
return all(items[i - 1] < items[i] for i in range(1, len(items))) # 0.53
return all(a < b for a, b in zip(items, items[1:])) # 1.32
return all(map(lambda a, b: a < b, items, items[1:])) # 1.34
return items == sorted(items) and len(items) == len(set(items)) # 0.81
def test_speed():
n, lst, st_time = 999999, [0] + list(range(20)), time()
for i in range(n):
x = is_ascending(lst)
print(f'{n} iterations, {time() - st_time:.3f} seconds')
if __name__ == '__main__':
print("Example:")
print(is_ascending([-5, 10, 99, 123456]))
# These "asserts" are used for self-checking and not for an auto-testing
assert is_ascending([-5, 10, 99, 123456]) == True
assert is_ascending([99]) == True
assert is_ascending([4, 5, 6, 7, 3, 7, 9]) == False
assert is_ascending([]) == True
assert is_ascending([1, 1, 1, 1]) == False
print("Coding complete? Click 'Check' to earn cool rewards!")
Oct. 19, 2021
Comments: