Skip to content

Instantly share code, notes, and snippets.

@tomwolber
Created February 8, 2013 20:52
Show Gist options
  • Save tomwolber/4741833 to your computer and use it in GitHub Desktop.
Save tomwolber/4741833 to your computer and use it in GitHub Desktop.
import math
# quick test cases
A = [-7, 1, 5, 2, -4, 3, 0]
B = [1, 2, 3, 4, 3, 2, 1]
C = [99, 0, 66, 32, 1]
D = [1, -1, 1, -1, 1, -1, 1]
E = [0,0,0]
F = [0,53,9]
G = [0,1,-1]
tests = [A,B,C,D,E,F,G]
def equi(A):
l = 0 # left of current index
r = 0 # right of current index
s = [] # list of eq. indices
for i in range(len(A)):
# get abs values of sum of numbers on either side of current indices
l = math.fabs(sum(A[:i]))
r = math.fabs(sum(A[i+1:]))
# add matches to list
if (l == r):
s.append(i)
if s:
return s
else:
return -1
if __name__ == "__main__":
for i in tests:
print i, equi(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment