def convert_radix(digits: list, base_in: int, base_out: int):
    """
    convert a non-negative number from radix base_in to radix base_out

    A n-digit input number is represented as [d_{n-1}..., d_4, d_3, d_2, d_1, d_0]
    The input number is \sum_{i=0}^{n-1}{d_i * base_in^i}
    We allow d_i<0, d_i=0, di>0 to represent balanced numeral systems;
    Note that base_in can be negative or positive as long as abs(base_in)>=2
    Examples of input numbers and output numbers:
    985 --> [9, 8, 5]
    -985 --> [-9, -8, -5] (handle this using upper-level list comprehensions, etc. This is different from balanced
    n-ary, because the all digits are non-positive)

    This algorithm works for any base with abs(base) >= 2

    This algorithm can also convert balanced n-ary to n-ary.
    """
    num = sum(d * base_in ** p for p, d in enumerate(digits[::-1]))
    if num == 0:
        return [0]
    result = []
    if base_out >= 2:
        if num > 0:
            while num != 0:
                num, rem = divmod(num, base_out)
                result.insert(0, rem)
        else:  # num < 0
            num = -num
            while num != 0:
                num, rem = divmod(num, base_out)
                result.insert(0, rem)
            result = [-x for x in result]
    elif base_out <= -2:  # negative base
        while num != 0:
            num, rem = divmod(num, base_out)
            if rem < 0:
                num += 1
                rem -= base_out
            result.insert(0, rem)
    return result