import math


def NIST_helper(L, N=None, p=None):
    """
    Computes percentile p of sorted list L of length N using definition in NIST handbook Sec 7.2.5.2.
    :param L: sorted list of values
    :param N: length of list
    :param p: percentile desired, in the interval [0,1], or None for median
    :return: computer percentile p of sorted list L
    """

    if N is None: N = len(L)

    if N < 1:
        return None
    elif N < 2:
        return L[0]

    if p is None:
        p = 0.5

    weight, base = math.modf(float(p) * (N + 1))
    base = int(base) - 1
    if base < 0:
        return L[0]
    elif base >= N - 1:
        return L[-1]
    else:
        return float(L[base]) + weight * (float(L[base + 1]) - float(L[base]))