Skip to content

Instantly share code, notes, and snippets.

@tomwolber
Last active December 12, 2015 07:09
Show Gist options
  • Save tomwolber/4734569 to your computer and use it in GitHub Desktop.
Save tomwolber/4734569 to your computer and use it in GitHub Desktop.
A = [-7, 1, 5, 2, -4, 3, 0]
def equi(A):
s = [] # array of equilibrium indices
for a in range(len(A)-1): # loop with '0 index' hack
x = A[:a+1] # split 'left' of the current index
y = A[a+2:] # split 'right' of the current index
n1 = 0 # this will be the total of the 'left' numbers
n2 = 0 # this will be the total of the 'right' numbers
for i in x: # add up numbers
n1 += i
for i in y: # add up numbers
n2 += i
if n1 == n2: # if two of these numbes are equal, add to array
s.append(a+1)
if len(s) > 0: # return indices if there are any, or return -1
return s
else:
return -1
print equi(A)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment