import logging


def allocate_colors(matches):
    """For the array of matching colors, build a new array that represents their relative allocations.
    As a side effect, modifies `matches` to add the percentages to each color.
    """
    # Allocate the colors by percentage to individual tiles
    total_points = sum([int(m.num_points) for m in matches])

    # Derive the percentage representation in the color set, throwing out any that are too rarely
    # represented to be displayable in our 10x10 grid
    for m in matches:
        logging.info("Total points {} / num points {} for {}".format(total_points, m.num_points, m.tile))
        
        alloc = int(m.num_points / total_points * 100)
        m.freq = alloc 
            
    for m in matches[:]:
        if m.freq == 0:
            matches.remove(m)

    # Sometimes the sums don't total 100 because we dropped out low representation items, so add those back
    sum_counts = sum(x.freq for x in matches)
    remainder = 100 - sum_counts
    matches.sort(key = lambda x: x.freq)
    matches.reverse()
    matches[-1].freq += remainder
    return matches