Alternate Signs

The list contains the same number of positive and negative integers, and you need to mix them by sign carefully. Write a function rearrange_array that returns a new list starting with a positive number, then alternating signs: positive, negative, positive, negative. Keep the relative order of the positive numbers, and also keep the relative order of the negative numbers.

Примеры

Ввод
[3, 1, -2, -5, 2, -4]
Вывод
[3, -2, 1, -5, 2, -4]
Ввод
[-1, 1]
Вывод
[1, -1]
Ввод
[1, -1, 2, -2]
Вывод
[1, -1, 2, -2]

Нужно реализовать

def rearrange_array(nums: list[int]) -> list[int]:

Теги

arraytwo-pointers
PostgreSQLv16

Siia ilmub koodi käivituse tulemus

Visible tests
1
Input
[3,1,-2,-5,2,-4]
Expected[3,-2,1,-5,2,-4]
2
Input
[-1,1]
Expected[1,-1]
3
Input
[1,-1,2,-2]
Expected[1,-1,2,-2]
4
Input
[-3,3]
Expected[3,-3]
5
Input
[5,-5,6,-6,7,-7]
Expected[5,-5,6,-6,7,-7]
6
Input
[2,-1,-3,4]
Expected[2,-1,4,-3]
Focus radio
Paused · SomaFM · Fluid