Tous les défis
Facile 0 résolus 0 % de réussite

Swap First and Last

Exchange the first and last items of a list.

Problème

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.

Contraintes

0 ≤ length ≤ 100,000

Exemples

Exemple 1
Entrée :
items = [1, 2, 3, 4]

Sortie attendue :
expected = [4, 2, 3, 1]
Entrée :
std::vector<long long> items = {1, 2, 3, 4};

Sortie attendue :
std::vector<long long> expected = {4, 2, 3, 1};
Entrée :
long[] items = new long[] {1, 2, 3, 4};

Sortie attendue :
long[] expected = new long[] {4, 2, 3, 1};

Explication

1 and 4 trade places: [4, 2, 3, 1].

Exemple 2
Entrée :
items = [9]

Sortie attendue :
expected = [9]
Entrée :
std::vector<long long> items = {9};

Sortie attendue :
std::vector<long long> expected = {9};
Entrée :
long[] items = new long[] {9};

Sortie attendue :
long[] expected = new long[] {9};

Explication

A single item has nothing to swap with: [9].

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.