Some of the classical problems in the programming domain come from different categories and one among them is finding the sum of subsets. This particular problem is also common when we need to accumulate the sum and store consecutive group summations. Let’s try different approaches to this problem in Python language.
Method #1: Using list comprehension + sum()
The list comprehension can be used to perform this particular task to filter out successive groups and the sum function can be used to get the summation of the filtered solution.
Python3
# Python3 code to demonstrate # Sectional subset sum in list # using list comprehension + sum() # initializing list test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ] # printing original list print ( "The original list : " + str (test_list)) # using list comprehension + sum() # Sectional subset sum in list res = [ sum (test_list[x: x + 3 ]) for x in range ( 0 , len (test_list), 3 )] # printing result print ( "The grouped summation list is : " + str (res)) |
The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14] The grouped summation list is : [19, 37, 44]
Time complexity: O(n), where n is the length of the input list. This is because the code iterates over the input list once, creating subsets of size 3 at each step.
Auxiliary space: O(m), where m is the number of subsets of size 3 in the input list. This is because the code creates a new list of size m to store the summed values of each subset.
Method #2 : Using sum() + itertools.islice()
The task of slicing the list into chunks is done by islice method here and the conventional task of getting the summation is done by the sum function as the above method.
Python3
# Python3 code to demonstrate # Sectional subset sum in list # using itertools.islice() + sum() import itertools # initializing list test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ] # printing original list print ( "The original list : " + str (test_list)) # using itertools.islice() + sum() # Sectional subset sum in list res = [ sum ( list (itertools.islice(test_list, i, i + 3 ))) for i in range ( 0 , len (test_list), 3 )] # printing result print ( "The grouped summation list is : " + str (res)) |
The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14] The grouped summation list is : [19, 37, 44]
Method #3 : Using itertools + sum()
To use the islice function from the itertools module, the built-in sum function, and a list comprehension to group the elements of the list into chunks of the desired size and sum each chunk, you can use the following approach:
Python3
from itertools import islice def sectional_subset_sum(lst, chunk_size): # Group the elements of the list into chunks of the desired size # and sum each chunk using a list comprehension return [ sum (chunk) for chunk in (islice(lst, i, i + chunk_size) for i in range ( 0 , len (lst), chunk_size))] # Test the function print (sectional_subset_sum([ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ], 3 )) # Printing result print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 2 )) print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 4 )) # This code is contributed by Edula Vinay Kumar Reddy |
[19, 37, 44] [3, 7, 11, 15, 9] [10, 26, 9]
Time complexity: O(n)
Auxiliary space: O(n)
Method 4: Using a loop to iterate over the list in chunks of the desired size and summing each chunk.
This function works similarly to the original one, but instead of using a list comprehension and itertools.islice to group the elements of the list into chunks, it uses a loop to iterate over the list in chunks of the desired size (specified by the chunk_size parameter) and sum each chunk using the built-in sum() function. The sums are stored in a list and returned at the end.
Algorithm:
- Define the function sectional_subset_sum(lst, chunk_size), which takes two arguments: lst, the list of integers to be grouped into chunks, and chunk_size, the desired size of each chunk.
- Initialize an empty list called result to store the sums of each chunk.
- Use a for loop to iterate over the indices of lst in steps of chunk_size. This will create a sequence of starting indices for each chunk.
- For each starting index i, create a slice of lst that starts at i and ends at i + chunk_size. This will extract a chunk of lst with chunk_size elements.
- Use the built-in sum() function to sum the elements of the current chunk.
- Append the sum of the current chunk to the result list using the append() method.
- After all, chunks have been processed, return the result list containing the sums of each chunk.
Python3
def sectional_subset_sum(lst, chunk_size): # Initialize an empty list to store the sums result = [] # Iterate over the list in chunks of chunk_size for i in range ( 0 , len (lst), chunk_size): # Sum the elements in the current chunk chunk_sum = sum (lst[i:i + chunk_size]) # Append the sum to the result list result.append(chunk_sum) # Return the result list return result # Output: [19, 37, 44] print (sectional_subset_sum([ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ], 3 )) # Output: [3, 7, 11, 15] print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 2 )) print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 4 )) # Output: [10, 22] |
[19, 37, 44] [3, 7, 11, 15, 9] [10, 26, 9]
Time complexity: O(n), where n is the length of the input list, since it it iterates over the list once.
Auxiliary space: O(n) since the function creates a list to store the sums.
Method #5: Using numpy.array_split() and numpy.sum()
Approach:
- Import the NumPy library.
- Convert the given list into a NumPy array using the np.array() function.
- Use the np.array_split() function to split the array into chunks of the desired size.
- Use the np.sum() function to calculate the sum of each chunk.
- Return the list of sums.
Python3
import numpy as np def sectional_subset_sum(lst, chunk_size): # Convert the list into a NumPy array arr = np.array(lst) # Split the array into chunks of chunk_size chunks = np.split(arr, len (lst) / chunk_size) # Calculate the sum of each chunk using np.sum() sums = [np. sum (chunk) for chunk in chunks] # Return the list of sums return sums # Example usage: # Output: [19, 37, 44] print (sectional_subset_sum([ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ], 3 )) # Output: [3, 7, 11, 15] print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 2 )) print (sectional_subset_sum([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ], 4 )) # Output: [10, 22] |
OUTPUT:
[19, 37, 44] [6, 9, 13, 17] [15, 30]
The time complexity of this program is O(n), where n is the length of the input list. The auxiliary space complexity is O(n), since the program creates an array of size n and a list of size n.
Method #6: Using a generator function and sum()
- Define a generator function sectional_subset_sum that takes two arguments: lst, the input list, and n, the size of each section.
- Use a for loop to iterate over the indices of the list, with a step size of n.
- In each iteration, yield the sum of the sublist from the current index to the next n indices.
- Convert the generator to a list using the list() function and store the result in res.
- Print the result.
Python3
def sectional_subset_sum(lst, n): for i in range ( 0 , len (lst), n): yield sum (lst[i:i + n]) test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ] res = list (sectional_subset_sum(test_list, 3 )) print ( "The grouped summation list is : " + str (res)) |
The grouped summation list is : [19, 37, 44]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1)