def NX(lengths, fraction):
    """ Calculates the N50 for a given set of lengths
    """
    lengths = sorted(lengths, reverse=True)
    tot_length = sum(lengths)
    cum_length = 0
    for length in lengths:
        cum_length += length
        if cum_length >= fraction*tot_length:
            return length