Prefix Sum Subarray Until False Value in Python



Python Programming is the most versatile programming which can be easily used as a high-level language. It brings down the difficulties faced in complex operations for handling data in large amounts. The array is a data structure that holds the elements within it and sub-arrays are the ones present inside the array structure. The array can hold elements of the same data type and are ordered in a square format. The size of the elements inside the array is also the same.

Python - Prefix sum Subarray till False value

In Python language, the variables are assigned to hold data but the array can store more amount of data at a time in a specified order. In this article, we are going to deal with the elements to add until the condition fails. The best way is to understand through an example,

0

1

2

3

4

5

6

7

1

4

8

0

10

5

2

1

Each element in the array is identified by its index value and the first element is retained in this case. The first and second are added and stored in the index value of 1. The second and third element is added and stored in the index value of 2. When the element is 0 in the array, it returns the result as 0 and the process continues.

Approach

  • Approach 1 ? Using the try and except method

  • Approach 2 ? Using the iteration method

Approach 1: Python Program to print the prefix sum of subarray till false value using the try and except method

The program to print the prefix sum of the initialized subarray till the false value is present involves the method of try and except. Some of the false values are 0, False, [], {}, 0.0 and many more.

Algorithm

  • Step 1 ? The list is initialized with the integer data type.

  • Step 2 ? The empty list is defined and then a variable is stored with a value of 0.

  • Step 3 ? The try and except method is used to iterate through each element of the list.

  • Step 4 ? When the condition is false which is zero, the loop breaks.

  • Step 5 ? With the help of the append function, the elements are added.

  • Step 6 ? The print function returns the final list.

Example

#initializing the list data structure
list1 = [2, 10, 4, 3, 0.0, 10, -2]
#empty list is initialized
arr = []
#the variable is declared as 0
num = 0
# Use the try and except method to print the sum until a false value occurs
try:
#for loop is used to iterate through the list
    for val in list1:
#To check whether the value is a false element
        if val == False:
            break
#When it is not a false element, then the values are added using the append function
        num += val
        arr.append(num)
#the print function returns the list
    print ("The array after product sum : " + str(arr))
except:
#When the false element is something like an empty list or strings it returns an error
    print("An error occurred")

Output

The array after product sum : [2, 12, 16, 19]

Approach 2: Python Program to print the prefix sum of subarray till false value using the iteration method

The element in the list undergoes the prefix product until the false value is present. To do this for loop is used to iterate through every element of the list.

Algorithm

  • Step 1 ? The list is initialized with an integer and a false element like 0.

  • Step 2 ? The empty is initialized and a new variable named "num" is assigned with a value of ?0'

  • Step 3 ? The for loop is used to traverse through the list of elements.

  • Step 4 ? When the condition is false which is zero, the loop breaks.

  • Step 5 ? When the result is not false, then it will add the num to arr.

  • Step 6 ? With the help of the append function, the elements are added.

  • Step 7 ? The print function will return the final list.

Example

#initializing the list with elements
list1 = [2, 10, 4, 3, 0, 10, -2]
#defining the empty list
arr = []
num = 0
#iteration through the list
for val in list1:
    if val == False:
        break
    num += val
    arr.append(num)
# It converts the list array to a string and [:] will create a copy of the list
print ("The array after prefix sum : " + str(arr[:]))

Output

The array after prefix sum : [2, 12, 16, 19]

Conclusion

Python language provides the user with versatile roles ranging from basic calculations to complex ones. When we say printing the prefix sum until a false value seems to be a simple task, but when creating a program, involves some tedious processes. The Python methods use various functionalities to print the list after the prefix sum.

Updated on: 2023-09-04T14:36:47+05:30

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements