Lesson 5.7 · Write it
Double every value
A functionPython or Java8 tests, 5 of them hidden
On a phone? Put the lines in order instead
Write a function that doubles every value in a list (an array in Java) of whole numbers, and returns the list.
- Each value is replaced by two times itself, and stays at its index.
- The list can be empty. An empty list comes back empty.
- Every value is between -1000 and 1000, and there are at most 20 values.
Only the list your function returns is checked.
def double_all(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.
- double_all([3, 8, 5]) should return [6, 16, 10]: three values, each doubled at its own index (rule 1)
- double_all([]) should return []: an empty list (rule 2)
- double_all([0, -4]) should return [0, -8]: zero stays zero, and a negative value doubles too (rule 1)
public static int[] doubleAll(int[] 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.
- doubleAll(new int[]{3, 8, 5}) should return [6, 16, 10]: three values, each doubled at its own index (rule 1)
- doubleAll(new int[]{}) should return []: an empty list (rule 2)
- doubleAll(new int[]{0, -4}) should return [0, -8]: zero stays zero, and a negative value doubles too (rule 1)