Created
February 8, 2013 20:52
-
-
Save tomwolber/4741833 to your computer and use it in GitHub Desktop.
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
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