Start learning free

LESSON 78 OF 415 · CONDITIONAL STATEMENTS

Allow to Contest in Swift: Range Check with &&

Practice Read the full explanation

At a contest, children can participate only if they have ages between the minimum age and the maximum age (both included). Given the age of a child, check if they are allowed or not.

Example 1:

Input: minAge = 7, maxAge = 12, age = 7
Output: true

Example 2:

Input: minAge = 3, maxAge = 8, age = 10
Output: false

Hints:

You can solve it in 3 different ways:
1. By checking for invalidations and return false when age is below the minimum age and when it is above the maximum age, otherwise return true

2. Using one if statement with 2 conditions and return true, otherwise false

3. Since the function return value is a boolean, instead of using an if, you just return the condition since its value is a boolean.
STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · CONDITIONAL STATEMENTS

    Allow To Contest in Swift, explained

    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:

    1. Reject first. Return false if age < minAge, return false if age > maxAge, and return true at the end.
    2. One if with two conditions, returning true inside it and false after it.
    3. Return the condition itself. A comparison joined with && is already a Bool, so no if is needed.

    The lesson uses the third, the shortest. It gives the same results as the others for every input.

    Step by step

    1. Write the lower-limit check: minAge <= age. It is true when the age is at or above the minimum. The <= keeps a child of exactly minAge in.
    2. 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.
    3. Join them with &&. The result is true only when both checks are true.
    4. 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 <= age is false, Swift doesn't evaluate the second comparison, as the answer is already false.
    • Ranges as an alternative. Swift's closed range minAge...maxAge has a contains method, so (minAge...maxAge).contains(age) says the same thing. It stops the program with an error when minAge is greater than maxAge, while the && version simply returns false, so the comparison version is the safer one here.

    Edge cases and common mistakes

    • Both limits are inclusive. allowToContest(7, 12, 7) and allowToContest(7, 12, 12) are both true. Using < instead of <= would reject them.
    • Just outside. allowToContest(7, 12, 6) and allowToContest(7, 12, 13) are both false.
    • 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) returns false, because no age is both at least 9 and at most 4.

    Testing

    • allowToContest(7, 12, 7) returns true.
    • allowToContest(3, 8, 10) returns false.
    • allowToContest(7, 12, 12) returns true.
    • allowToContest(7, 12, 6) returns false.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free