I'm one of those who don't clear the whole thing, but anyway, at least I can propose to you a solution of "brute force" that is totally feasible. The idea is:Each basket may eventually have 0 to 20 eggs (21 possibilities)We can then randomly generate an important but reasonable number computationally of 6 possible basketsThen we simply check the assumptions of each combination: the sum of the baskets should be 80This would be the idea:import random
def get_random_combination(canastas, maximo_x_canasta):
combinacion = []
for c in range(canastas):
combinacion.append(random.randint(0, maximo_x_canasta+1))
return combinacion
def is_valid_combination(combinacion, maximo_x_canasta, total_huevos):
if sum(combinacion) != total_huevos:
return False
return True
for intentos in range(1000):
combinacion = get_random_combination(6, 20)
if is_valid_combination(combinacion, 20, 80):
print(combinacion, sum(combinacion))
[17, 9, 18, 18, 18, 0] 80
[12, 9, 7, 18, 21, 13] 80
[20, 16, 13, 4, 12, 15] 80
[17, 20, 19, 10, 9, 5] 80
[20, 18, 20, 11, 7, 4] 80
[15, 14, 17, 18, 15, 1] 80
[10, 14, 18, 1, 17, 20] 80
[12, 9, 20, 8, 21, 10] 80
[14, 17, 6, 21, 11, 11] 80
[16, 12, 10, 20, 15, 7] 80
[20, 20, 11, 16, 7, 6] 80
[10, 18, 15, 12, 18, 7] 80
[19, 11, 16, 1, 14, 19] 80
[21, 15, 11, 19, 11, 3] 80
[17, 7, 13, 14, 19, 10] 80
[20, 20, 9, 1, 13, 17] 80
[18, 7, 18, 12, 20, 5] 80
[14, 8, 19, 11, 13, 15] 80
[17, 15, 20, 11, 12, 5] 80
[9, 9, 17, 12, 17, 16] 80
[11, 16, 8, 14, 21, 10] 80
[4, 13, 5, 20, 20, 18] 80
As you can see, making a simulation of 1000 possible combinations of baskets we have found several possibilities that meet our assumptions. A possibility of improving this algorithm is to end when we find the first successful combination, which is what is required, for example:import random
def try_to_get_valid_random_combination(intentos_maximos, canastas, maximo_x_canasta, total_huevos):
for intento in range(intentos_maximos):
combinacion = [random.randint(0, maximo_x_canasta+1) for c in range(canastas)]
if sum(combinacion) == total_huevos:
return combinacion
return None
combinacion = try_to_get_valid_random_combination(1000, 6, 20, 80)
And, by the way, if you're looking for a dictionary, you can transform any of the combination lists you have:d = {}
for i, v in enumerate(combinacion,1):
d["canasta{0}".format(i)] = v