How to make all possible combinations to arrive at a proposed result?

2

Well, let's start at the beginning, by the origin, the beginning of the beginning.

I have this program:

from itertools import combinations
lis = [1,2,3,4,5,6,7,8,9]
for i in (3, len(lis)):
    for comb in combinations(lis, i):
       if sum(comb) == 10:
           print (comb,'= 10')

The function of it, is nothing more than: Add a specific number of numbers within a list, so that you get the result set.

Let's suppose that the list has 9 numbers, from 1 to 9. lis = [1,2,3,4,5,6,7,8,9]
And I want the number of numbers added together, be 3. for i in (3, len(lis)):
And I also determined a result, in this case, the number 10.% with%

Obviously the output of the program will be this:

if sum(comb) == 10:
(1, 2, 7) = 10
(1, 3, 6) = 10
(1, 4, 5) = 10

Run the program and see for yourself:

link

Briefly, in a succinct and synthetic way, it is the sum of all possible combinations of a given number of numbers within the list that will give the proposed result.

Now, knowing what it is, we can continue to apply this knowledge.

Guess where it will be used?

That'sright,inamagicsquare!

Forthosewhodonotyetknowwhatamagicsquareis,here'stheexplanation:

MagicSquareisasquaretableequaltotheintersectionofnumbersinwhichthesumofeachcolumn,eachrow,andthetwodiagonalsarethesame.

Seetheexample:

Continuing:

Nowlookhowinteresting,toapplyintheprogramI"started" the diagonals of the square.

Now,areyouseeing"these" results? (2, 3, 5) = 10

I'm going to apply in the program with the amount (107,113,119,150,131,137,143,149,141,133,150,117,109,101) the numbers of the matrix to sum all the possible combinations that will give "these" results, with the exception of 150 that will be with the quantity "5" .

Run the program, and do not worry, it will take less than 30 seconds to find all the results:

link

Now finally the Magna question!

How do you make all possible combinations of the sums of all these results obtained in this program regroup so that the array looks exactly like this?

Theprogramwouldprintallthepossibilitiesthatcometotheseresultswiththearrayexactlythatway,withthenumbersinsidethearrayandtheirresultsaround.

UpdatedProgram:

link

What needs to be done in it is that these "results" repeat the same thing that the "combinations" function did to find these "results", but this time adding 7 in 7 and the if sum (comb) must be equal to the final_result ..

"6"
for comb4 in combinations(lista, 7):

if it had printed an array like this:

MATRIX

#....[47][16][41][10][35]....
#[05]....[48][17][42]....[29]
#[30][06]....[49]....[36][12]
#[13][31][07]....[43][19][37]
#[38][14]....[01]....[44][20]
#[21]....[08][33][02]....[45]
#....[15][40][09][34][03]....

and all the possibilities of this matrix .. that give the same results ...

FINAL RESULT

#[107][113][119][150][131][137][143]
#...................................[149]
#...................................[141]
#...................................[133]
#...................................[150]
#...................................[117]
#...................................[109]
#...................................[101]
    
asked by anonymous 03.01.2018 / 00:02

0 answers