• + 0 comments

    This test took me few hours to solve. I learned some techniques to solve quartiles problems but they don't work. Finally I could solve this way:

    def get_quartile_position(number: float, is_even: float = False) -> [int, tuple]:
        if is_even:
            number = int(number)
            return number, number + 1
        return round(number)
    
    
    def get_positions(total_population: int) -> tuple[any, any, any]:
        even_population = total_population % 2 == 0
        has_even_quartiles = (total_population // 2) % 2 == 0
        total_population += 1
        q1_position = total_population / 4
        q1_position = get_quartile_position(q1_position, has_even_quartiles)
        q2_position = total_population / 2
        q2_position = get_quartile_position(q2_position, even_population)
        q3_position = (3 * total_population) / 4
        q3_position = get_quartile_position(q3_position, has_even_quartiles)
    
        return q1_position, q2_position, q3_position
    
    
    def get_quartiles_values(group: dict, pos: [int, tuple]) -> int:
        if type(pos) is tuple:
            return round((group[pos[0]] + group[pos[1]]) / 2)
    
        return group[pos]
    
    
    def quartiles(arr) -> list[int]:
        arr.sort()
        n = len(arr)
        arr = dict(enumerate(arr, start=1))
        return list(map(lambda pos: get_quartiles_values(arr, pos), get_positions(n)))