Interleave Two Halves
Imagine a list made of two halves that need to be woven together one item at a time. Given a list nums of length 2n, where the first half comes before the second half, return a new list that alternates between them. Take the first item from the first half, then the first item from the second half, then the second from each half, and so on.
Примеры
Ввод
[2, 5, 1, 3, 4, 7], 3Вывод
[2, 3, 5, 4, 1, 7]Ввод
[1, 2, 3, 4, 4, 3, 2, 1], 4Вывод
[1, 4, 2, 3, 3, 2, 4, 1]Ввод
[1, 1, 2, 2], 2Вывод
[1, 2, 1, 2]Нужно реализовать
def shuffle(nums: list[int], n: int) -> list[int]:
Теги
array
Prihláste sa, aby ste videli históriu odoslaní
Prihlásiť saPrihláste sa, aby ste mohli používať AI mentora
Prihlásiť saVisible tests
1
Input
[2,5,1,3,4,7], 3Expected
[2,3,5,4,1,7]2
Input
[1,2,3,4,4,3,2,1], 4Expected
[1,4,2,3,3,2,4,1]3
Input
[1,1,2,2], 2Expected
[1,2,1,2]4
Input
[5,10], 1Expected
[5,10]5
Input
[1,2,3,4,5,6], 3Expected
[1,4,2,5,3,6]6
Input
[2,5,1,3,4,7,2], 0Expected
[]Focus radio
Paused · SomaFM · Fluid