make_ends

Given a list of ints, return a new list length 2 containing the first and last elements from the original list. The original list will be length 1 or more.

This exercise was taken from codingbat.com and has been adapted for the Python language. There are many great programming exercises there, but the majority are created for Java.

Test 1

Input:

[1, 2, 3]

Output:

[1, 3]

Test 2

Input:

[1, 2, 3, 4]

Output:

[1, 4]

Test 3

Input:

[7, 4, 6, 2]

Output:

[7, 2]

Test 4

Input:

[1, 2, 2, 2, 2, 2, 2, 3]

Output:

[1, 3]

Test 5

Input:

[7, 4]

Output:

[7, 4]

Test 6

Input:

[7]

Output:

[7, 7]

Test 7

Input:

[5, 2, 9]

Output:

[5, 9]

Test 8

Input:

[2, 3, 4, 1]

Output:

[2, 1]