4
\$\begingroup\$

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. P is an index of an array A.

Input:

A[0] = -1
A[1] =  3
A[2] = -4
A[3] =  5
A[4] =  1
A[5] = -6
A[6] =  2
A[7] =  1

Output:

1
3
7

1 is an equilibrium index of this array, because:

A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]

3 is an equilibrium index of this array, because:

A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

7 is also an equilibrium index, because:

A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0

My solution for review:

def isEquilibrium(alist, ind):
    listLen = len(alist)
    left = getSum(0, ind, alist)
    right = getSum(ind+1, listLen, alist)
    # print left, right
    if left == right:
        return True

def getSum(start, end, lis):
    tot = 0
    for i in xrange(start, end):
        tot = tot + lis[i]
    return tot

def solution(alist):
    for i in xrange(0, len(alist)):
        result = isEquilibrium(alist, i)
        if(result):
            print i

solution([-1,3,-4,5,1,-6,2,1])
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

You can utilize built-in features of Python to refactor the code.

  1. Use the sum() function

    Python already provides a sum() function to add all elements of a list that you can use.

    example_list = [1, 2, 3, 4, 5]
    result = sum(example_list)
    
  2. Slicing

    Python allows selecting a range of values of a list. The syntax is:

    list[start:end:step]
    

    All these are optional. By default, start is 0, end is the length of the list, and step is 1.

    Using this, you can select elements before the index by list[:index] and select elements after the index using list[index+1:].

  3. Idiomaticity

    Here, a and b are compared for equality. If a is equal to b, the expression a == b will be True, so True is returned.

    if a == b:  
        return True
    
    # Refactored
    return a == b
    
  4. Naming conventions

    snake_case is preferred over camelCase for naming in Python community. Use is_equilibrium instead of isEquilibrium.

\$\endgroup\$
3
  • \$\begingroup\$ Why don't you use a list comprehension in your last function ? More, please add more context to your answer.Thanks ! \$\endgroup\$ Feb 19, 2017 at 12:07
  • \$\begingroup\$ To stay close to the approach by the OP. Can you clarify regarding the context? @dex-ter \$\endgroup\$
    – user131449
    Feb 19, 2017 at 12:11
  • \$\begingroup\$ Welcome on Code Review. This answer is pretty close to a code dump, which is frowned upon here. Instead, explain your reasonning and why your changes makes your code better so the OP can learn from your thought process. \$\endgroup\$ Feb 19, 2017 at 12:58
4
\$\begingroup\$

Some things to note here:

  1. The name solution is not a good name. Solution of what?. It needs to be more specific and meaningful like equilibrium or equilibrium_points or find_equilibrium_points.
  2. You don't need to do if(result) as if result is equivalent.
  3. Python has a build-in sum method that takes care of array sums. So the function getSum is redundant.
  4. Try to use snake case for function names such as is_equilibrium.
  5. The logic of the isEquilibrium function can be simplified like this:

    def is_equilibrium(alist, ind):
         return sum(alist[:ind]) == sum(alist[ind+1:])
    
  6. Lastly while the answer you specified is correct is not efficient. It has \$O(n^2)\$ time complexity. You can achieve \$O(n)\$ time complexity by iterating only twice on the list one for finding the sum and one for printing the equilibrium points. I'm not going to provide you an answer to that as a simple search will show you the algorithm. That's not the point of the review either.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.