-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathair12.py
67 lines (47 loc) · 1.66 KB
/
air12.py
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
import sys
from typing import Optional
# Fonctions utilisées:
def quick_sort(numbers: list[int], left: int = 0, right: Optional[int] = None) -> None:
if right is None:
right = len(numbers) - 1
if left < right:
partition_pos = partition(numbers, left, right)
quick_sort(numbers, left, partition_pos - 1)
quick_sort(numbers, partition_pos + 1, right)
def partition(numbers: list[int], left: int, right: int) -> int:
pivot = numbers[right]
i = left - 1
for j in range(left, right):
if numbers[j] <= pivot:
i += 1
numbers[i], numbers[j] = numbers[j], numbers[i]
numbers[i + 1], numbers[right] = numbers[right], numbers[i + 1]
return i + 1
# Gestion d'erreurs :
def is_valid_length(arguments: list[str]) -> bool:
if len(arguments) < 2:
print("Erreur : Merci d'indiquer deux arguments qui doivent etre des nombres entiers")
return False
return True
def is_digit(arguments: list[str]) -> bool:
for argument in arguments:
if not argument.isdigit():
print("Erreur : Merci d'indiquer uniquement des nombres entiers")
return False
return True
# Récupération de données :
def get_arguments() -> list[str]:
arguments = sys.argv[1:]
return arguments
# Résolution :
def display_list_quick_sorted() -> None:
arguments = get_arguments()
if not is_valid_length(arguments):
return
if not is_digit(arguments):
return
numbers = list(map(int, arguments))
quick_sort(numbers)
print(" ".join(map(str, numbers)))
# Affichage :
display_list_quick_sorted()