Deviding N(1,2,3,..,N) part from numeric list as summation of each values(don't sorted) has highest as possible.

K. Elo maillists at pp.inet.fi
Mon Oct 10 13:29:32 EDT 2016


Hi!

Here one possible solution:

--- snip ---

land = [10,20,30,40,110,50,18,32,5]

landlength=len(land)
winnersplit=[]

for i in range(landlength-2):
     for j in range(landlength-1-i):
         splitsums=[sum(land[0:(i+1)]), sum(land[(i+1):(i+j+2)]), 
sum(land[(i+j+2):landlength])]
 
differences=abs(splitsums[1]-splitsums[0])+abs(splitsums[2]-splitsums[1])

         if (len(winnersplit)==0 or 
differences<(abs(sum(winnersplit[1])-sum(winnersplit[0]))+abs(sum(winnersplit[2])-sum(winnersplit[1])))):
             winnersplit=[land[0:(i+1)], land[(i+1):(i+j+2)], 
land[(i+j+2):landlength]]
             print(splitsums," <<< WE HAVE A NEW LEADER! >>>")
         else:
             print(splitsums, " <<< split differences too large :( >>>")

print("And the winner is ... ", winnersplit)

--- snip ---

HTH,
Kimmo

10.10.2016, 19:25, Nuen9 kirjoitti:
>> Hi!
>>
>> Could it be, "Nuen9", that you would like to find a split where the
>> split sums are close to each other? In other words, you define the
>> number of splits (in your example: 3) and the algortihm should test all
>> possible combinations and select the split where the sum differences are
>> smallest.
>>
>> Best,
>> Kimmo
>
> Yes it is, I want example python code for finding my answers but I don't code my answers. please help me.
>



More information about the Python-list mailing list