Largest of three

Easy
The course
Number Best so far, Branching
1

Understand

Before writing anything, get clear on what is being asked.

Think of it like this

It is a knockout tournament. The first number is the reigning champion. Each other number challenges it — if the challenger is bigger, it becomes the new champion. Whoever is champion at the end wins.

This "best so far" idea scales without changing shape: the same three lines find the largest of a million numbers, which is exactly what you will do in week 3.

Write a function that takes three whole numbers and gives back whichever is largest.

If two or three of them tie for biggest, give back that value — it is still the largest.

Example 1

Given a = 3, b = 9, c = 4

Answer 9

Example 2

Given a = 10, b = 2, c = 5

Answer 10

Example 3

Given a = 1, b = 1, c = 1

Answer 1

All equal, so that value is the largest.

What you can rely on

  • All three are whole numbers
  • They may be negative
2

Write it

Write the function in the editor, then run it against the examples.

The editor on the right already has the function signature filled in for you. Write your answer inside it and press Run — that checks your code against the examples above only, so you can experiment freely.

Run your code to tick this step off.

Stuck? Take one hint at a time

Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.

3

Own it

Submit against every test, then check you can explain why it works.

Keep the best so far

O(1) time · O(1) space

Hold a variable for the biggest you have seen. Compare each remaining value against it and replace it whenever you find something bigger.

The champion approach
biggest = a
if b > biggest:
    biggest = b
if c > biggest:
    biggest = c
return biggest

Why start `biggest` at a, rather than at 0?

Press Submit to run every test, including the hidden ones.