This lesson contains extra practice exercises, some of which are challenging. If you get stuck, feel free to skip to the next problem or lesson and come back later. You can always use the My Progress page to review all past exercises.
The first exercise is about debugging. It tries to use a formula that is not quite correct to compute population growth.
The rest of the exercises in this lesson are about the min and max functions.
Simplifying a complex expression
Multiple Choice Exercise: Simplification
What is a simplification of the following expression?
max(x - 3, min(x + 10, x + 5))
Correct! Since x + 5 is always smaller than x + 10, we can simplify min(x + 10, x + 5) to just x + 5. Then working our way outwards, the entire expression is max(x - 3, x + 5). Since x + 5 is always the maximum of these two numbers, that is the result.
Complicating a simple expression
Payment Calculator
Sorting Scramble
The final problem is a challenging problem, about sorting numbers in a weird way. There are better, simpler, and faster sorting methods that you can learn about after completing our introductory lessons.
If you get stuck, feel free to skip the problem. You can always come back to it later. Additionally, you can keep track of what you have or have not finished by visiting the My Progress page.
Continue on to lesson 3!
This is a tricky problem. Think about just one year for starters. What is the population after 1 year of 10% increase? The number of new people is 10% of 1000 = 0.1 * 1000 = 100. So after one year the total (original + new) = 1100. But the sample program is wrong even for one year:
prints 100.0 instead of the correct 1100.0. Do you see how to change the second line by one character to give an output of 1100 (the total) instead of 100?
What is max(-A, -B)?
What is min(A, B)+max(A, B)?
Break the problem into two parts. First compute the maximum of 0.021 times balance, and 10. This is the payment unless it exceeds the balance. Then use min somehow.
This code uses a particular strategy which affects the pair (x, y) first, then (y, z), then (x, y) again.