I could really use a hint in the right direction on this one. My code seems to be stuck at test 5(values: 12,30,30,32,42,49).
def checkio(data): left = [] right = [] data.sort(reverse=True) for weight in data: print ('weight ', weight) if sum(left) <= sum(right): left.append(int(weight)) pass elif sum(right) <= sum(left): right.append(int(weight)) pass print ('left ', left, sum(left)) print ('right ', right, sum(right)) return abs(sum(left) - sum(right)) #abs to get rid of negative numbers
The print statements are not necessary, but they were put in to help me see the flow of the code.
So, originally I had the idea to simply run a loop and sort each number into whatever list had a lower sum than the other, as this seemed like a logical way to waiting a balance between the two lists. I realized that ideally I should sort by the largest number first though, in the event there is a singe number that is drastically larger than the others(I.E. 10,30,100).
In the case of test five my code is sorting this way:
left[49,30,12] right[42.32.30]
but 30 and 32 need to swap lists to give the correct answer. I am terrible at math so its very well possible this is more of a math issue than knowing the language. I have the idea that maybe after I've sorted the numbers into the two lists that I should run a follow up comparison. I know you can work with two lists with the zip() function, but I really can't figure how I would go about doing that even with the zip function. Can anyone suggest a function that might help me here, or possibly a logic hint that I've missed?