Contents

Description

You’re responsible for providing a demographic report for your local school district based on age. To do this, you’ll determine which ‘category’ each person fits into based on their age:

From 0 to 2: ‘Infant’
From 3 to 4: ‘Preschool’
From 5 to 11: ‘Elementary school’
From 12 to 14: ‘Middle school’
From 15 to 18: ‘High school’
From 19 to 22: ‘College’
From 23 to 65: ‘Working’
From 66 to 100: ‘Retired’

My solution

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
44
45
46
47
48
from collections import OrderedDict
from typing import Union


class Solution:
    """Solution using an `OrderedDict()`.

    `OrderedDict()` guarantees that keys maintain the sequential order
    in which they were originally declared.

    Attributes:
        categories: An `OrderedDict()` representing the minimum age
                    for each category and an associated category name.

    """

    categories = OrderedDict(
        {
            66: "Retired (ha!)",
            23: "Working",
            19: "College",
            15: "High school",
            12: "Middle school",
            5: "Elementary school",
            3: "Preschool",
            0: "Infant",
        }
    )

    def categorize(self, age: Union[int, float]) -> str:
        """Determine the category name for the given age.

        Args:
            age: A person's age
        Returns:
            The corresponding category name as a string.

        Examples:
            >>> s = Solution(); s.categorize(3)
            'Preschool'
            >>> s.categorize(15.5)
            'High school'

        """
        for c in self.categories:
            if age >= c:
                return self.categories[c]
        return ""

Tests

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from hypothesis import given
from hypothesis import strategies as st
from typing import Union

from solution import Solution

s = Solution()


age_ranges = (
    ((0, 3), "Infant"),
    ((3, 5), "Preschool"),
    ((5, 12), "Elementary school"),
    ((12, 15), "Middle school"),
    ((15, 19), "High school"),
    ((19, 23), "College"),
    ((23, 66), "Working"),
    ((66, 99), "Retired (ha!)"),
)


def test_returntype():
    """Should return a value of the appropriate type."""
    assert isinstance(s.categorize(0), str), "expected return type str"


def test_invalid_age():
    """Given an invalid age, should return an empty string."""
    assert s.categorize(-1) == ""


def test_age_ranges():
    """Should work for every integer value in `age_ranges`."""
    for elt in age_ranges:
        category = elt[1]
        age_range = elt[0]
        for age in range(*age_range):
            assert (
                s.categorize(age) == category
            ), f"expected {category} for age {age}"


def test_fractional_ages():
    """Should work for every fractional value in `age_ranges`."""
    for elt in age_ranges:
        lower = elt[0][0]
        age = lower + 0.5
        category = elt[1]
        assert (
            s.categorize(age) == category
        ), f"expected {category} for age {age}"


@given(
    st.one_of(
        st.integers(min_value=66, max_value=99),
        st.fractions(min_value=66.0, max_value=99.0),
    )
)
def test_property_test(age: Union[int, float]):
    """Should work for values generated by a property testing tool."""
    assert s.categorize(age) == "Retired (ha!)"