Organizing Containers of Balls

  • + 0 comments
    def organizingContainers(container) -> str:
        '''returns Impossible | Possible for whether the balls can be split'''
        # this problem is very easy with two containers
        # the count of balls in one container must be equal to the counts of one type
        
        # this logic can be extended to the fact that for every count of a ball type there needs to be a container that fits it
        # in other words the sum of the columns must be equal to the sum of some other row (and that row cannot already be filled with a different ball type)
        
        # if you are wondering why zip(*container) gives a list of a ball type's count in each container here is the breakdown
        # container = [
        #      [1,4],
        #      [2,3],
        # ]
        # the * operator unpacks the container matrix 
        # zip(*container) = zip([1,4], [2,3])
        #
        # zip then joins equal indices together
        # list(zip(*container)) = [(1,2), (4,3)]
        sums_of_balls = [sum(b) for b in zip(*container)]
        sums_of_containers = [sum(c) for c in container]
        sums_of_balls.sort()
        sums_of_containers.sort()
        return "Possible" if sums_of_balls == sums_of_containers else "Impossible"