Created
November 6, 2016 16:42
-
-
Save Wingless-Archangel/ea04eddd0ac2d5317b8d55006d4bdaad to your computer and use it in GitHub Desktop.
find the least difference between 2 part of the same list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you can write to stdout for debugging purposes, e.g. | |
# print "this is a debug message" | |
def solution(A): | |
# write your code in Python 2.7 | |
# Split list into 2 list | |
if len(A) == 0: | |
return 0 | |
if len(A) == 1: | |
return A[0] | |
i = 1 | |
left = A[0] | |
right = sum(A) - left | |
result = abs(left-right) | |
while(i <= len(A)-1): | |
# print "i = %d" % i | |
diff = abs(left - right) | |
print "diff = %d" % diff | |
if diff < result: | |
result = diff | |
left += A[i] | |
right -= A[i] | |
i += 1 | |
print result | |
return result | |
solution([4,1,2,4,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment