def is_relationship_active_at_timestamp(relationship, ts):
    """Check if a relationship is active at a given timestamp"""

    def _check_period(p_start, p_end):
        """Check if a given timestamp is inside a period"""
        if p_start <= ts <= p_end:
            return True
        return False

    # Iterate over an even list of timestamps
    def _iterate_over_periods(periods_ts_list):
        """Check if a given timestamp is inside one of the periods of a relationship"""
        iter_periods = iter(periods_ts_list)
        for p in iter_periods:
            if _check_period(p, next(iter_periods)):
                return True
        return False

    relationship_periods = relationship.get("periods", [])

    # If no periods are found, consider the relationship active
    if not relationship_periods:
        return True

    if len(relationship_periods) % 2 == 0:
        # If we have an even number of periods, we have an array of finished periods and we need to
        # iterate over them to determine if the given timestamp is inside one of them
        return _iterate_over_periods(relationship_periods)
    else:
        # If we have an odd number of periods, we have an array of finished periods and a last period started
        # without an end, so we need to check first if the timestamp is after the last period without an end
        # and if not, iterate over the other periods (this code also works when we only have one timestamp inside
        # the periods
        last_period_ts = relationship_periods[-1]
        if ts >= last_period_ts:
            return True
        return _iterate_over_periods(relationship_periods[:-1])