Python Nozioni di baseFacile

Ordina la prima metà in modo crescente e la seconda metà in modo discendente

Guida dettagliata e implementazione Python per il problema "Ordina la prima metà ascendente e la seconda metà discendente".

Dichiarazione del problema

Facile

Scrivi una funzione sort_half(arr) che prenda un elenco di numeri interi e restituisca un nuovo elenco in cui la prima metà è ordinata in ordine crescente e la seconda metà in ordine decrescente. Se l'array ha lunghezza dispari, l'elemento centrale appartiene alla prima metà. Ad esempio, per la lunghezza 5, i primi 3 elementi vengono ordinati in ordine crescente e gli ultimi 2 in ordine decrescente.

Vincoli
  • 1 <= len(arr) <= 10^5
  • -10^9 <= arr[i] <= 10^9

Esempi

Example 1
Input
arr = [5, 2, 8, 1, 4, 7]
Output
[1, 2, 5, 8, 7, 4]
Explanation

First half [5,2,8] sorted ascending: [1,2,5]. Second half [1,4,7] sorted descending: [8,7,4]. Wait — we split the original array: first 3 elements [5,2,8] sort ascending -> [2,5,8], last 3 [1,4,7] sort descending -> [7,4,1]. Result: [2,5,8,7,4,1].

Example 2
Input
arr = [3, 1, 2, 5, 4]
Output
[1, 2, 3, 5, 4]
Explanation

First half (3 elements) [3,1,2] sorted ascending: [1,2,3]. Second half (2 elements) [5,4] sorted descending: [5,4]. Result: [1,2,3,5,4].

Example 3
Input
arr = [9, 3, 6, 1]
Output
[3, 9, 6, 1]
Explanation

First half [9,3] ascending: [3,9]. Second half [6,1] descending: [6,1]. Result: [3,9,6,1].

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche degli array come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.