Last active
May 19, 2017 19:16
-
-
Save mdomke/8d041f2d8b5b657e7727693fb86939cc to your computer and use it in GitHub Desktop.
equi function in python
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
def equi(l): | |
"""Finds the equilibirum point in a list `l`. | |
The equilibrium point is the index within the list where the sum of | |
elements to the left is equal to the sum of the elements to the right. | |
Args: | |
l (list): A list of integers. | |
Returns: | |
int or None: The index of the equilibrium point if there is one. | |
""" | |
left, right = 0, sum(l) | |
for i, n in l: | |
if left == right: | |
return i | |
left += n | |
right -= n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment