Thought I’d run this by you guys, if that’s okay?
Job is to take an integer array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.
def find_even_index(arr): #taking index length, checking if even or odd, and then dividing it by 2 to get midpoint, or close to midpoint of list y = len(arr) if y % 2 != 0 and y != 1: x = (y-1)/2 else: x = y/2 print "x is %s" % x #summing up each item of arr list, then dividing by 2 to get a value. a= 0 for z in range(len(arr)): a += arr[z] b = a/2 #checking if b is equal to any item of the arr list, by going through its indices for N in arr: if b == arr[N]: print "Iterator N is %d" % N print "b is %d" % b print "Index value is %d" % arr[N] return True if N == range(len(arr)) and b != arr[N]: print "-1" return -1
So why doesn’t it return correctly for me?