Problem overview
The method allowToContest takes three integers: minAge, maxAge and age. It returns true when the child's age is between minAge and maxAge, including both limits, and false otherwise.
In the first example, the limits are 7 and 12 and the child is 7, so the answer is true: the limit itself counts. In the second, the limits are 3 and 8 and the child is 10, which is above the maximum, so the answer is false.
Approach: two comparisons joined by and
An age is allowed when two things hold at once: it is at least minAge and at most maxAge. That is a job for the and operator &&. There are three equivalent ways to write it:
- Reject first. Return
falseifage < minAge, returnfalseifage > maxAge, and returntrueat the end. - One
ifwith two conditions, returningtrueinside it andfalseafter it. - Return the condition itself. A comparison joined with
&&is already aBool, so noifis needed.
The lesson uses the third, the shortest. It gives the same results as the others for every input.
Step by step
- Write the lower-limit check:
minAge <= age. It is true when the age is at or above the minimum. The<=keeps a child of exactlyminAgein. - Write the upper-limit check:
age <= maxAge. It is true when the age is at or below the maximum, and again the limit itself is allowed. - Join them with
&&. The result is true only when both checks are true. - Return the whole expression.
Written with the smaller value on the left each time, the condition reads like a mathematical range: minAge <= age <= maxAge. Swift doesn't accept that chained form, which is why the two comparisons are separate.
The solution in Swift
class Solution {
func allowToContest(_ minAge: Int, _ maxAge: Int, _ age: Int) -> Bool {
return minAge <= age && age <= maxAge
}
}
Swift details worth knowing:
- Precedence. Comparisons bind tighter than
&&, so the line means(minAge <= age) && (age <= maxAge)without parentheses. - Underscores. Each parameter has an underscore before its name, so a call has no labels:
allowToContest(7, 12, 7). The order matters: minimum, maximum, then the age. - Short-circuiting. If
minAge <= ageis false, Swift doesn't evaluate the second comparison, as the answer is alreadyfalse. - Ranges as an alternative. Swift's closed range
minAge...maxAgehas acontainsmethod, so(minAge...maxAge).contains(age)says the same thing. It stops the program with an error whenminAgeis greater thanmaxAge, while the&&version simply returnsfalse, so the comparison version is the safer one here.
Edge cases and common mistakes
- Both limits are inclusive.
allowToContest(7, 12, 7)andallowToContest(7, 12, 12)are bothtrue. Using<instead of<=would reject them. - Just outside.
allowToContest(7, 12, 6)andallowToContest(7, 12, 13)are bothfalse. - Using
||instead of&&. With "or", almost every age passes, because any age is either above the minimum or below the maximum. - Mixed-up parameters. Passing the age in the wrong position changes the meaning of the checks.
- An impossible range.
allowToContest(9, 4, 5)returnsfalse, because no age is both at least 9 and at most 4.
Testing
allowToContest(7, 12, 7)returnstrue.allowToContest(3, 8, 10)returnsfalse.allowToContest(7, 12, 12)returnstrue.allowToContest(7, 12, 6)returnsfalse.
