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.
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.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
Hold a variable for the biggest you have seen. Compare each remaining value against it and replace it whenever you find something bigger.
biggest = a
if b > biggest:
biggest = b
if c > biggest:
biggest = c
return biggestWhy start `biggest` at a, rather than at 0?
Run your code to see the test results.
⌘/Ctrl + Enter to run · ⌘/Ctrl + Shift + Enter to submit
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.
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.
Each one nudges you a little further. There is no reveal-the-answer button — working it out is the part that actually teaches you.
Submit against every test, then check you can explain why it works.
Hold a variable for the biggest you have seen. Compare each remaining value against it and replace it whenever you find something bigger.
biggest = a
if b > biggest:
biggest = b
if c > biggest:
biggest = c
return biggestWhy start `biggest` at a, rather than at 0?