Skip to content

Instantly share code, notes, and snippets.

@montaro
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montaro/a187932a620ae0cf7b50 to your computer and use it in GitHub Desktop.
Save montaro/a187932a620ae0cf7b50 to your computer and use it in GitHub Desktop.
List Equilibrium Index
__author__ = 'arefaey'
def list_equilibrium(l):
s = sum(l)
right_sum = s
left_sum = 0
for i in range(len(l)):
elem = l[i]
right_sum = right_sum - elem
if right_sum == left_sum:
print "The Equilibrium Index is: ", i, "for list: ", l
return i
left_sum = left_sum + elem
print "There is no Equilibrium Index for list: ", l
return None
l0 = []
l1 = [1]
l2 = [1, 2]
l3 = [1, 2, 1]
l4 = [1, 2, 1, -3, -4, 3, 5, 22, 1, 7, -2, -1]
test_lists = [l0, l1, l2, l3, l4]
for l in test_lists:
list_equilibrium(l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment