regarding your python code: do you know about 'enumerate'? It returns an iterable of (index, value) pairs of a sequence. :)
def equi ( A ):
right = sum(A)
left = 0
for i,x in enumerate(A):
right -= x
if right == left:
return i
left += x
return -1
What's interesting is that all the versions I've seen so far are pretty much identical apart from very minor things. This would seem to me to be obviously the correct way to do it but I'm interested if it's just a way. Did anyone solve this in a different way of equal or greater elegance? for i in xrange(len(A)):
if right[i] == left[i]:
return i
The final loop here is slightly tighter, so as long as your parallelism didn't add big overheads, this will be faster.Original: http://pastebin.com/m6e742f56 Parallelised: http://pastebin.com/m32a60b1