def propsample(freqs, num):
    """Proportionally samples from the given frequencies.
    Returns a list of same length with the number of times each index should be
    sampled such that the total number of elements sampled is `num`.
    """
    lens = [int(f*num)+1 for f in freqs]
    total = 0
    for i, l in enumerate(lens):
        if l+total > num:
            lens[i] = num-total
        total += lens[i]
    return lens