Python | Multiplication till Null value
Last Updated :
05 May, 2023
The prefix array is quite famous in the programming world. This article would discuss a variation of this scheme. This deals with the cumulative list product till a False value, and again starts cumulation of product from occurrence of True value. Let’s discuss certain way in which this can be performed.
Method #1 : Using Naive Method In the naive method, we just construct the new list comprising of the product of prev. value of list until False and restarts the procedure once a True value is encountered.
Python3
test_list = [ 1 , 3 , 4 , False , 4 , 5 , False , 7 , 8 ]
print ("The original list is : " + str (test_list))
for i in range ( 1 , len (test_list)):
if test_list[i]:
test_list[i] * = test_list[i - 1 ]
else :
test_list[i] = 1
print ("The computed modified new list : " + str (test_list))
|
Output :
The original list is : [1, 3, 4, False, 4, 5, False, 7, 8]
The computed modified new list : [1, 3, 12, 1, 4, 20, 1, 7, 56]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), extra space of size n is required
Method#2: Using Recursive method.
The function multiply_till_null() takes in the input list test_list and the starting index index as parameters. If the index is equal to the length of the list, it returns the modified list. If the element at the current index is null, it sets the value to 1. Otherwise, it multiplies the current element with the previous element in the list. The function then recursively calls itself with the updated list and index incremented by 1. Finally, the function is called with the input list and starting index of 1 to begin the recursion.
Python3
def multiply_till_null(test_list, index):
if index = = len (test_list):
return test_list
if not test_list[index]:
test_list[index] = 1
else :
test_list[index] * = test_list[index - 1 ]
return multiply_till_null(test_list, index + 1 )
test_list = [ 1 , 3 , 4 , False , 4 , 5 , False , 7 , 8 ]
print ( "The original list is:" , test_list)
result = multiply_till_null(test_list, 1 )
print ( "The computed modified new list:" , result)
|
Output
The original list is: [1, 3, 4, False, 4, 5, False, 7, 8]
The computed modified new list: [1, 3, 12, 1, 4, 20, 1, 7, 56]
The time complexity of this method is O(n) because the function is called recursively n times, where n is the length of the input list.
The auxiliary space is also O(n) because the function call stack grows with each recursive cal
Method #3: Using Iterative Method
The iterative method involves iterating over the input list and multiplying the elements until a null value is encountered. We can use a for loop to iterate over the list and keep track of the previous element in a variable. Whenever a null value is encountered, we set the current element to 1, and continue with the iteration.
step-by-step approach
Initialize a variable prev to 1.
Iterate over the elements of the input list using a for loop.
For each element in the list, check if it is null. If it is null, set it to 1. If it is not null, multiply it with the prev variable and update the element in the list.
Update the prev variable with the current element in the list.
Return the modified list.
Python3
def multiply_till_null(test_list):
prev = 1
for i in range ( len (test_list)):
if not test_list[i]:
test_list[i] = 1
else :
test_list[i] * = prev
prev = test_list[i]
return test_list
test_list = [ 1 , 3 , 4 , False , 4 , 5 , False , 7 , 8 ]
print ( "The original list is:" , test_list)
result = multiply_till_null(test_list)
print ( "The computed modified new list:" , result)
|
Output
The original list is: [1, 3, 4, False, 4, 5, False, 7, 8]
The computed modified new list: [1, 3, 12, 1, 4, 20, 1, 7, 56]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1), as we are modifying the input list in-place and using a constant amount of extra space for the prev variable.
Similar Reads
Python | Tuple multiplication
Sometimes, while working with records, we can have a problem in which we may need to perform multiplication of tuples. This problem can occur in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression The combination of abov
5 min read
Multiplication Table Using While Loop in Python
Multiplication tables are fundamental in mathematics, serving as the building blocks for more advanced concepts. In Python, we can use various methods to generate multiplication tables, and one versatile tool for this task is the 'while' loop. In this article, we will explore some commonly used and
3 min read
Python | Values till False element
Many a times we require to find the first occurring False number to begin the processing with. This has mostly use case in Machine Learning domain in which we require to process data excluding None or 0 values. Letâs discuss certain ways in which this can be performed. Method #1 : Using next() + enu
4 min read
Python | Multiply Dictionary Value by Constant
Sometimes, while working with dictionaries, we can have a use-case in which we require to multiply a particular keyâs value by K in dictionary. It may seem a quite straight forward problem, but catch comes when the existence of a key is not known, hence becomes a 2 step process at times. Letâs discu
3 min read
Reverse Multiplication Table Using For loop in Python
A multiplication table of any number can be printed using the For loop in Python. We can also print the multiplication table in reverse order using a for loop in Python. In this article, we will see how we can print the multiplication table in reverse order using a for loop in Python. Example: Input
3 min read
Python - Constant Multiplication over List
We are given a list we need to multiply each element in the list by a constant. For example, we are having a list a = [1, 2, 3, 4, 5] and constant c=2 we need to multiply this constant in whole list. Using List ComprehensionList comprehension allows us to multiply each element in list by a constant
3 min read
Python | Remove None values from list
Removing None values from a list in Python is a common task when cleaning or processing data. This process helps to filter out unwanted None elements, leaving only the meaningful values in the list for further use. There can be multiple methods to remove None values from a Python list without removi
3 min read
Python | Check if tuple has any None value
Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains all valid values i.e has any None value. This kind of problem is common in data preprocessing steps. Let's discuss certain ways in which this task can be performed. Method #1 : U
5 min read
Python | K Value Indices Product
Usually, we require to find the index, in which the particular value is located. There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in list and compute their product. Lets discuss certain w
7 min read
How to Round Floating Value to Two Decimals in Python
In this article, we will round off a float value in Python to the nearest two decimal places. Python provides us with multiple approaches to format numbers to 2 decimal places. Using round() round() function is the most efficient way to round a number to a specified number of decimal places. It dire
2 min read