Last active
August 29, 2015 14:16
-
-
Save montaro/a187932a620ae0cf7b50 to your computer and use it in GitHub Desktop.
List Equilibrium Index
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
__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