Compute the qth percentile of the data along the specified axis.
    Simpler version than the numpy version that always flattens input arrays.

    Examples
    --------
    >>> a = [[10, 7, 4], [3, 2, 1]]
    >>> percentile(a, 20)
    2.0
    >>> percentile(a, 50)
    3.5
    >>> percentile(a, [20, 80])
    [2.0, 7.0]
    >>> a = list(range(40))
    >>> percentile(a, 25)
    9.75

    :param a: Input array or object that can be converted to an array.
    :param q: Percentile to compute, which must be between 0 and 100 inclusive.
    :return: the qth percentile(s) of the array elements.

def percentile(a, q):
    """
    Compute the qth percentile of the data along the specified axis.
    Simpler version than the numpy version that always flattens input arrays.

    Examples
    --------
    >>> a = [[10, 7, 4], [3, 2, 1]]
    >>> percentile(a, 20)
    2.0
    >>> percentile(a, 50)
    3.5
    >>> percentile(a, [20, 80])
    [2.0, 7.0]
    >>> a = list(range(40))
    >>> percentile(a, 25)
    9.75

    :param a: Input array or object that can be converted to an array.
    :param q: Percentile to compute, which must be between 0 and 100 inclusive.
    :return: the qth percentile(s) of the array elements.
    """
    if not a:
        return None

    if isinstance(q, (float, int)):
        qq = [q]
    elif isinstance(q, (tuple, list)):
        qq = q
    else:
        raise ValueError("Quantile type {} not understood".format(type(q)))

    if isinstance(a, (float, int)):
        a = [a]

    for i in range(len(qq)):
        if qq[i] < 0. or qq[i] > 100.:
            raise ValueError("Percentiles must be in the range [0,100]")
        qq[i] /= 100.

    a = sorted(flatten(a))
    r = []

    for q in qq:
        k = (len(a) - 1) * q
        f = math.floor(k)
        c = math.ceil(k)
        if f == c:
            r.append(float(a[int(k)]))
            continue
        d0 = a[int(f)] * (c - k)
        d1 = a[int(c)] * (k - f)

        r.append(float(d0 + d1))

    if len(r) == 1:
        return r[0]
    return r