Skip to content
ExamSpine

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.

  1. Each value is replaced by two times itself, and stays at its index.
  2. The list can be empty. An empty list comes back empty.
  3. 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)

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)
solution.pySolution.java