"""
    An example of counting sub-multisets of fixed cardinality of a given multiset using the 
    M&Ms pocket example.
    """

    msg_1 = (
        "Given a pocket of 10 M&Ms, divided in the 6 classical colours "
        "3 red, 2 orange , 1 yellow, 4 blue, 3 brown and 3 green, how "
        "many different handful of 7 "
        "candies can we extract?"
    )

    n_1 = 7
    t_up1 = [3, 2, 1, 4, 3, 3]

    answer_1 = number_of_k_resolutions_upper_constraints(n_1, t_up1)
    list_1 = k_resolutions_upper_constraints_list(n_1, t_up1)

    msg_2 = (
        "Given a pocket of 30 M&Ms, divided in the 6 classical colours"
        "11 red, 9 orange , 6 yellow, 14 blue, 3 brown and 17 green, how many \
            different handful of 12 candies can we extract?"
    )

    n_2 = 12
    t_up2 = [11, 9, 6, 14, 3, 17]

    answer_2 = number_of_k_resolutions_upper_constraints(n_2, t_up2)

    i_have_a_lot_of_spare_time = False
Exemplo n.º 2
0
    print 'closed form            = ' + str(closed_form)
    print 'counting the solutions = ' + str(counting_solutions)
    print 'It is ' + str(closed_form == counting_solutions) + ' that the closed form equals the direct enumerations. \n'

    #########################################
    # k -resolutions of n upper constraints #
    #########################################

    n_upper = 10
    t_upper = [4, 3, 1, 5, 5]  # [5, 7, 8, 5, 4, 2]
    k_upper = len(t_upper)

    time_results = [0.0] * 2

    start = time.time()
    closed_form = number_of_k_resolutions_upper_constraints(n_upper, t_upper)
    time_results[0] += (time.time() - start)

    start = time.time()
    counting_solutions = len(k_resolutions_upper_constraints_list(n_upper, t_upper))
    time_results[1] += (time.time() - start)

    print '\n' + str(k_upper) + '-resolutions of ' + str(n_upper) + ', with upper constraints ' + str(t_upper) + ':'
    print 'closed form            = ' + str(closed_form)
    print 'counting the solutions = ' + str(counting_solutions)
    print 'It is ' + str(closed_form == counting_solutions) + ' that the closed form equals the direct enumerations. \n'

    print 'Computational time for upper resolutions constraints: '
    print 'with closed form: ' + str(time_results[0]) + ' sec.'
    print 'with direct enumeration: ' + str(time_results[1]) + ' sec.'