Skip to content
ExamSpine

Lesson 4.6 · Write it

Largest of three

A functionPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a function that returns the largest of three whole numbers.

  1. a, b and c are whole numbers that can be negative.
  2. Two or all three may be equal. If the largest value appears more than once, return it.

Write a helper function that returns the larger of two numbers, and use it.

Only the number your function returns is checked.

def max_of_three(a: int, b: int, c: int) -> int

You cannot use these in this question:

  • max()
  • min()
  • sorted()

This question is about writing the comparison yourself, so built-in max functions are not allowed.

Examples

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

  • max_of_three(1, 2, 3) should return 3: the third is largest (rule 1)
  • max_of_three(5, 5, 5) should return 5: all three equal (rule 2)
  • max_of_three(9, 3, 7) should return 9: the first is largest (rule 1)

public static int maxOfThree(int a, int b, int c)

Checking whether Java answers can run here.

You cannot use these in this question:

  • Math.max()
  • Math.min()
  • Collections.max()
  • Collections.min()

This question is about writing the comparison yourself, so built-in max functions are not allowed.

Examples

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

  • maxOfThree(1, 2, 3) should return 3: the third is largest (rule 1)
  • maxOfThree(5, 5, 5) should return 5: all three equal (rule 2)
  • maxOfThree(9, 3, 7) should return 9: the first is largest (rule 1)
solution.pySolution.java