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.
- The list has at least one value.
- 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)
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.
- 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)