Problema
Return a copy of the list with its first and last elements swapped. Lists with fewer than two elements stay the same.
Implement swap_first_last(items).
Input
items: The list of integers.
Output
Return the list with the ends swapped.
Restrições
0 ≤ length ≤ 100,000
Exemplos
Exemplo 1
Entrada:
items = [1, 2, 3, 4]
Saída esperada:
expected = [4, 2, 3, 1]
Entrada:
std::vector<long long> items = {1, 2, 3, 4};
Saída esperada:
std::vector<long long> expected = {4, 2, 3, 1};
Entrada:
long[] items = new long[] {1, 2, 3, 4};
Saída esperada:
long[] expected = new long[] {4, 2, 3, 1};
Explicação
1 and 4 trade places: [4, 2, 3, 1].
Exemplo 2
Entrada:
items = [9]
Saída esperada:
expected = [9]
Entrada:
std::vector<long long> items = {9};
Saída esperada:
std::vector<long long> expected = {9};
Entrada:
long[] items = new long[] {9};
Saída esperada:
long[] expected = new long[] {9};
Explicação
A single item has nothing to swap with: [9].
Referências
Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.