'Python to split an array and check sum of all elements in two arrays is same

Team,

my code is below and I DON'T want to use modules because am trying to learn all the logic.

I am trying to slit Array into two sub arrays, then sum all its elements to check if their elements sums are equal. I am getting error, may be am not using right syntax of class and object. can anyone hint please.

class Class_SplitArray_FindSums:
def splitArray(self,array):
    middle_index = len(array)//2
    self.first_half = array[:middle_index]
    self.second_half = array[middle_index:]
    print(self.first_half, self.second_half)
    return self.first_half, self.second_half
    

def sumOfSplitArrays(self):
    for ele1 in self.first_half:
        sum_of_ele1 = ele1 + self.first_half[ele1 + 1]
    print(sum_of_ele1)

    for ele2 in self.second_half:
        sum_of_ele2 = ele2 + self.second_half[ele2 + 1]
    print(sum_of_ele2)

    if sum_of_ele1 == sum_of_ele2:
        return True
    else:
        return False

my_array=[1,2,3,4,5,6,7,8,9]
classObj_SplitArray_FindSums = Class_SplitArray_FindSums()
classObj_SplitArray_FindSums.splitArray(my_array)
classObj_SplitArray_FindSums.sumOfSplitArrays()

output:

    Traceback (most recent call last):
  File "/Users/team/code/Persi/python/SplitArrayFindifTheirSumEq.py", line 35, in <module>
    classObj_SplitArray_FindSums.sumOfSplitArrays()
  File "/Users/team/code/Persi/python/SplitArrayFindifTheirSumEq.py", line 19, in sumOfSplitArrays
    sum_of_ele1 = ele1 + self.first_half[ele1 + 1]
IndexError: list index out of range


Solution 1:[1]

You have 2 little bugs in your code.

  1. You used self.second_half in the loop of the first_half
  2. You don't even need to access elements in the array inside your loop. Since the ele variable already contains the value of the element inside the array.

Solution 2:[2]

Finally figured out. thanks for above Answer1.

from array import *

class Class_SplitArray_FindSums:
    my_array=[32,45,67,100,101,102]
  
    
    def splitArray(self,array):
        middle_index = len(array)//2
        self.first_half = array[:middle_index]
        self.second_half = array[middle_index:]
        print(self.first_half, self.second_half)
        return self.first_half, self.second_half

    def sumOfSplitArrays(self):
        sums_from_loop = 0

        for ele1 in self.first_half:
            print(ele1) #prints direct value not index.
            sums_from_loop = sums_from_loop + ele1
        sums_from_loop_ele1 = sums_from_loop
        print('sum of elements from first half', sums_from_loop_ele1)
        
        for ele2 in self.second_half:
            print(ele2)
            sums_from_loop = sums_from_loop + ele2
        sums_from_loop_ele2 = sums_from_loop
        print('sum of elements from second half', sums_from_loop_ele2)
        
        if sums_from_loop_ele2 == sums_from_loop_ele1:
            print('equal')
        else:
            print('notequal')


        
ObjofClass_SplitArray_FindSums = Class_SplitArray_FindSums()
ObjofClass_SplitArray_FindSums.splitArray(ObjofClass_SplitArray_FindSums.my_array)
ObjofClass_SplitArray_FindSums.sumOfSplitArrays()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Sides
Solution 2