Skip to content
ExamSpine

Lesson 5.5 · Write it

Last value

A functionPython or Java8 tests, 5 of them hidden

On a phone? Put the lines in order instead

Write a function that returns the last value in a list (an array in Java) of whole numbers.

  1. The list has at least one value.
  2. Return the value at the last index.

Only the value your function returns is checked.

def last_value(values: list[int]) -> int

Examples

Your function is tested on 8 cases: the 3 examples below, and 5 hidden ones that check the rules above.

  • last_value([3, 8, 5]) should return 5: the last of three values (rule 2)
  • last_value([7]) should return 7: one value is both the first and the last (rule 1)
  • last_value([9, 4, 1]) should return 1: the last value is the smallest one (rule 2)

public static int lastValue(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.

  • lastValue(new int[]{3, 8, 5}) should return 5: the last of three values (rule 2)
  • lastValue(new int[]{7}) should return 7: one value is both the first and the last (rule 1)
  • lastValue(new int[]{9, 4, 1}) should return 1: the last value is the smallest one (rule 2)
solution.pySolution.java