Python PodstawyŁatwe

Sortuj pierwszą połowę rosnąco, a drugą połowę malejąco

Szczegółowy przewodnik i implementacja Python dla problemu „Sortuj pierwszą połowę rosnąco i drugą połowę malejąco”.

Oświadczenie o problemie

Łatwe

Napisz funkcję sort_half(arr), która pobiera listę liczb całkowitych i zwraca nową listę, w której pierwsza połowa jest posortowana w porządku rosnącym, a druga w porządku malejącym. Jeśli tablica ma nieparzystą długość, środkowy element należy do pierwszej połowy. Na przykład dla długości 5 pierwsze 3 elementy są sortowane rosnąco, a ostatnie 2 są sortowane malejąco.

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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla tablic, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.