Skip to content

Problem Solving with Selection

Problem Set

The following problem sets are intended for practice. Attempt these on your own.

Largest of Three

??? example "Problem" ### Problem Description

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Given three _real_ numbers $n_1$, $n_2$, and $n_3$, we want to find the largest of the three.
Note that the largest one may not be unique, but we simply want the largest one.
See the sample below.

=== "Sample #1"
    - $n_1 = 0.1$
    - $n_2 = 0.3$
    - $n_3 = 0.2$

    Largest is $0.3$

=== "Sample #2"
    - $n_1 = -0.1$
    - $n_2 = -0.2$
    - $n_3 = -0.3$

    Largest is $-0.1$

=== "Sample #3"
    - $n_1 = 0$
    - $n_2 = 0$
    - $n_3 = 0$

    Largest is $0$


### Task

Write Python code to compute and print the largest of the three number.


### Assumptions

- `#!py3 n1`, `#!py3 n2`, and `#!py3 n3` are floating point numbers.
- `#!py3 n1`, `#!py3 n2`, and `#!py3 n3` are already initialized.

??? hints "Hints" ??? hint "Hint #1" === "Hint" What is the condition to know if \(n_1\) is the largest number? Can you figure out the other conditions?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    === "Answer"
        ```py
        n1 >= n2 and n1 >= n2
        ```

        Note that we need to use `#!py3 >=` because there is a possibility there are two numbers that are equal.

??? hint "Hint #2"
    === "Hint"
        What constructs do you need to use?
        What about for the subsequenc check?

    === "Answer"
        We need at least one if-statement.
        For the other, we need to use `#!py3 elif` because there is a possibility there are two numbers that are equal.

??? success "Possible Solution"
    ```py
    # Assume n1, n2, and n3 are initialized
    if n1 >= n2 and n1 >= n3:
      print(n1)  # n1 is largest
    elif n2 >= n1 and n2 >= n3:
      print(n2)  # n2 is largest
    elif n3 >= n1 and n3 >= n2:
      print(n3)  # n3 is largest
    ```

    There is an alternative solution by assuming that `#!py3 n1` is the largest.
    Then we challenge that by checking if `#!py3 n2` is the current largest.
    Finally,  we challenge that by checking if `#!py3 n3` is the current largest.
    The advantage of this technique is that it can be extended easily to more numbers without changing the condition too much.
    Can you write the code based on this idea?

    ??? coding "Assume Largest First"
        ```py
        # Assume n1, n2, and n3 are initialized
        largest = n1     # assume n1 is largest
        if largest < n2: # check if n2 is larger
          largest = n2
        if largest < n3: # check if n3 is larger
          largest = n3
        print(largest)
        ```

Waist-to-Hip Ratio

??? example "Problem" ### Problem Description

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Waist-to-hip ratio (WHR) is an alternative to body mass index (BMI) indicator to measure if a given person is of normal weight, overweight, or obese.
The computation requires two values:

- Waist measurement in cm.
- Hip measurement in cm.

First we compute the ratio of waist to hip and determine the weight indicator using the table below.
Note that the indicator depends on whether the patient is male or female.

| Male       | Female     | Indicator   |
|------------|------------|-------------|
| < 0.9      | < 0.8      |  Normal     |
| 0.9 - 1.00 | 0.8 - 0.85 |  Overweight |
| > 1.00     | > 0.85     |  Obese      |


### Task

Write Python code to compute and print weight indicator given three variables `#!py3 is_male`, `#!py3 waist`, and `#!py3 hip`.


### Assumptions

- `#!py3 is_male` is a boolean.
- `#!py3 waist` and `#!py3 hip` are positive floating point numbers (_i.e.,_ `#!py3 waist > 0` and `#!py3 hip > 0`).
- `#!py3 waist` and `#!py3 hip` are given in cm.

??? hints "Hints" ??? hint "Hint #1" === "Hint" Can you compute the waist-to-hip ratio?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    === "Answer"
        ```py
        ratio = waist / hip
        ```

??? hint "Hint #2"
    === "Hint"
        Let us use nested if-statements.
        Consider checking `#!py3 is_male` first.
        What should be the inner condition?

    === "Answer"
        ```py
        # assume is_male is True
        if ratio < 0.9:
          # Normal
        elif 0.9 <= ratio and ratio <= 1.00:
          # Overweight
        else:
          # Obese
        ```

        The condition for when `#!py3 is_male` is `#!py3 False` is left as an exercise to the reader.

??? success "Possible Solution"
    ```py
    # Assume is_male, waist, and hip are initialized
    ratio = waist / hip
    if is_male:
      if ratio < 0.9:
        print('Normal')
      elif 0.9 <= ratio and ratio <= 1.00:
        print('Overweight')
      else:
        print('Obese')
    else:
      if ratio < 0.8:
        print('Normal')
      elif 0.8 <= ratio and ratio <= 0.85:
        print('Overweight')
      else:
        print('Obese')
    ```