-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.py
More file actions
67 lines (50 loc) · 1.83 KB
/
sorting.py
File metadata and controls
67 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
sorting.py - Core Sorting Module
Contains the main sorting functionality that can be called from anywhere.
"""
from typing import List, Any, Union
def sort_data(data: List[Any], reverse: bool = False) -> List[Any]:
if not data:
return []
try:
return sorted(data, reverse=reverse)
except TypeError as e:
# Handle case where items aren't comparable
print(f"Warning: Unable to sort mixed incompatible types: {e}")
return data
def sort_data_inplace(data: List[Any], reverse: bool = False) -> None:
if not data:
return
try:
data.sort(reverse=reverse)
except TypeError as e:
print(f"Warning: Unable to sort mixed incompatible types: {e}")
def is_sorted(data: List[Any], reverse: bool = False) -> bool:
if len(data) <= 1:
return True
if reverse:
return all(data[i] >= data[i + 1] for i in range(len(data) - 1))
else:
return all(data[i] <= data[i + 1] for i in range(len(data) - 1))
# Module-level test function
def test_sorting_module():
print("Testing sorting module...")
# Test numeric data
numbers = [64, 34, 25, 12, 22, 11, 90]
sorted_numbers = sort_data(numbers)
print(f"Original: {numbers}")
print(f"Sorted: {sorted_numbers}")
print(f"Is sorted: {is_sorted(sorted_numbers)}")
# Test string data
words = ['zebra', 'apple', 'banana', 'cherry']
sorted_words = sort_data(words)
print(f"Words: {words} -> {sorted_words}")
# Test mixed data
mixed = [3.14, 42, 'apple', 1.41]
try:
sorted_mixed = sort_data(mixed)
print(f"Mixed: {mixed} -> {sorted_mixed}")
except Exception as e:
print(f"Mixed data error: {e}")
if __name__ == "__main__":
test_sorting_module()