Lesson 5.6 · Write it
Move the first to the end
A functionPython or Java8 tests, 5 of them hidden
On a phone? Put the lines in order instead
Write a function that takes the first value out of a list of whole numbers, adds it to the end, and returns the list.
- The list has at least one value.
- Every other value moves down one index.
- A list of one value comes back unchanged.
- In Java, the list is an
ArrayList<Integer>.
Only the list your function returns is checked.
def move_first_to_end(values: list[int]) -> list[int]
Examples
Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.
- move_first_to_end([3, 8, 5]) should return [8, 5, 3]: the 3 leaves the front and joins the end (rule 2)
- move_first_to_end([7]) should return [7]: one value (rule 3)
- move_first_to_end([1, 2]) should return [2, 1]: two values change places (rule 2)
public static ArrayList<Integer> moveFirstToEnd(ArrayList<Integer> values)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.
- moveFirstToEnd([3, 8, 5]) should return [8, 5, 3]: the 3 leaves the front and joins the end (rule 2)
- moveFirstToEnd([7]) should return [7]: one value (rule 3)
- moveFirstToEnd([1, 2]) should return [2, 1]: two values change places (rule 2)