Легко Решено: 0 Процент решений: 0%

Swap First and Last

Exchange the first and last items of a list.

Условие

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.

Ограничения

0 ≤ length ≤ 100,000

Примеры

Пример 1
Ввод:
items = [1, 2, 3, 4]

Ожидаемый вывод:
expected = [4, 2, 3, 1]
Ввод:
std::vector<long long> items = {1, 2, 3, 4};

Ожидаемый вывод:
std::vector<long long> expected = {4, 2, 3, 1};
Ввод:
long[] items = new long[] {1, 2, 3, 4};

Ожидаемый вывод:
long[] expected = new long[] {4, 2, 3, 1};

Пояснение

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

Пример 2
Ввод:
items = [9]

Ожидаемый вывод:
expected = [9]
Ввод:
std::vector<long long> items = {9};

Ожидаемый вывод:
std::vector<long long> expected = {9};
Ввод:
long[] items = new long[] {9};

Ожидаемый вывод:
long[] expected = new long[] {9};

Пояснение

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

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.