From 31c6e633c89a8a729da37e356144efda0e447156 Mon Sep 17 00:00:00 2001 From: Louis Chmielewski <louis.chmielewski@icloud.com> Date: Wed, 10 Apr 2024 09:26:36 +0200 Subject: [PATCH] merge_sort is_sorted --- Tp09/merge_sort.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Tp09/merge_sort.py b/Tp09/merge_sort.py index 0c016e0..91b5736 100755 --- a/Tp09/merge_sort.py +++ b/Tp09/merge_sort.py @@ -125,7 +125,24 @@ def is_sorted(l: ApLst, comp: Callable[[T, T], int]=compare) -> bool: $$$ is_sorted(native_to_list([1, 2, 4, 3])) False """ - ... + if l.is_empty(): + return True + + current = l.head() + rest = l.tail() + + while not rest.is_empty(): + next_element = rest.head() + if comp(current, next_element) > 0: + return False + current = next_element + rest = rest.tail() + + return True +# compteur = 0 +# while not l.is_empty and l.comp(l.head(), is_sorted(l.tail()) == -1): +# compteur = compteur + 1 +# return compteur == length(l) def split(l: ApLst) -> tuple[ApLst, ApLst]: -- GitLab