1
+ # Description:
2
+ # Implement a Python function that checks the order of the elements in a list. The function should return:
3
+
4
+ # 1 if the list is strictly increasing,
5
+ # -1 if the list is strictly decreasing,
6
+ # 0 if the list is constant or contains mixed order elements.
7
+
8
+ def check_list_order (lst ):
9
+ if not lst or len (lst ) < 2 :
10
+ return 0
11
+
12
+ is_increasing = True
13
+ is_decreasing = True
14
+
15
+ for i in range (1 , len (lst )):
16
+ if lst [i ] < lst [i - 1 ]:
17
+ is_increasing = False
18
+ elif lst [i ] > lst [i - 1 ]:
19
+ is_decreasing = False
20
+ else :
21
+ is_increasing = False
22
+ is_decreasing = False
23
+
24
+ if is_increasing :
25
+ return 1
26
+ elif is_decreasing :
27
+ return - 1
28
+ else :
29
+ return 0
30
+
31
+
32
+ def check_list_order_with_sorted (lst ):
33
+ if not lst or len (lst ) < 2 :
34
+ return 0
35
+
36
+ sorted_asc = sorted (lst )
37
+ sorted_desc = sorted (lst , reverse = True )
38
+
39
+ if lst == sorted_asc and len (set (lst )) == len (lst ):
40
+ return 1
41
+ elif lst == sorted_desc and len (set (lst )) == len (lst ):
42
+ return - 1
43
+ else :
44
+ return 0
45
+
46
+
47
+ def test_lists ():
48
+ assert check_list_order ([1 , 2 , 3 , 4 ]) == 1 , "Expected 1 for strictly increasing list [1, 2, 3, 4]"
49
+ assert check_list_order ([4 , 3 , 2 , 1 ]) == - 1 , "Expected -1 for strictly decreasing list [4, 3, 2, 1]"
50
+ assert check_list_order ([1 , 1 , 1 , 1 ]) == 0 , "Expected 0 for constant list [1, 1, 1, 1]"
51
+ assert check_list_order ([1 , 3 , 2 , 4 ]) == 0 , "Expected 0 for mixed order list [1, 3, 2, 4]"
52
+ assert check_list_order ([]) == 0 , "Expected 0 for empty list []"
53
+ assert check_list_order ([5 ]) == 0 , "Expected 0 for single element list [5]"
54
+
55
+ assert check_list_order_with_sorted ([1 , 2 , 3 , 4 ]) == 1 , "Expected 1 for strictly increasing list [1, 2, 3, 4]"
56
+ assert check_list_order_with_sorted ([4 , 3 , 2 , 1 ]) == - 1 , "Expected -1 for strictly decreasing list [4, 3, 2, 1]"
57
+ assert check_list_order_with_sorted ([1 , 1 , 1 , 1 ]) == 0 , "Expected 0 for constant list [1, 1, 1, 1]"
58
+ assert check_list_order_with_sorted ([1 , 3 , 2 , 4 ]) == 0 , "Expected 0 for mixed order list [1, 3, 2, 4]"
59
+ assert check_list_order_with_sorted ([]) == 0 , "Expected 0 for empty list []"
60
+ assert check_list_order_with_sorted ([5 ]) == 0 , "Expected 0 for single element list [5]"
0 commit comments