Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let’s discuss certain ways in which this problem can be performed.
Method #1 : Using re.split() This task can be solved using the split functionality provided by Python regex library which has power to split the string according to certain conditions and in this case all numbers and operators.
Python3
# Python3 code to demonstrate working of # Splitting operators in String # Using re.split() import re # initializing string test_str = "15 + 22 * 3-4 / 2" # printing original string print ( "The original string is : " + str (test_str)) # Using re.split() # Splitting operators in String res = re.split(r '(\D)' , test_str) # printing result print ( "The list after performing split functionality : " + str (res)) |
The original string is : 15+22*3-4/2 The list after performing split functionality : [’15’, ‘+’, ’22’, ‘*’, ‘3’, ‘-‘, ‘4’, ‘/’, ‘2’]
Time Complexity: O(n) where n is the length of the input string.
Auxiliary Space: O(n)
Method #2 : Using re.findall() This Python regex library function also can perform the similar task as the above function. It can also support decimal numbers as additional functionality.
Python3
# Python3 code to demonstrate working of # Splitting operators in String # Using re.findall() import re # initializing string test_str = "15 + 22.6 * 3-4 / 2" # printing original string print ( "The original string is : " + str (test_str)) # Using re.findall() # Splitting operators in String res = re.findall(r '[0-9\.]+|[^0-9\.]+' , test_str) # printing result print ( "The list after performing split functionality : " + str (res)) |
The original string is : 15+22.6*3-4/2 The list after performing split functionality : [’15’, ‘+’, ‘22.6’, ‘*’, ‘3’, ‘-‘, ‘4’, ‘/’, ‘2’]
Method #3: Using split() method
Python3
# Python3 code to demonstrate working of # Splitting operators in String # initializing string test_str = "15 + 22.6 * 3-4 / 2" # printing original string print ( "The original string is : " + str (test_str)) # Splitting operators in String op = "+-*/" res = "" for i in test_str: if i in op: res + = "@" + i + "@" else : res + = i res = res.split( "@" ) # printing result print ( "The list after performing split functionality : " + str (res)) |
The original string is : 15 + 22.6 * 3-4 / 2 The list after performing split functionality : ['15 ', '+', ' 22.6 ', '*', ' 3', '-', '4 ', '/', ' 2']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Approach#4: Using while loop
this approach is a simple algorithm that involves iterating through the input string character by character, identifying operators and operands, and storing them in a list
Algorithm
1. Initialize an empty list to store the operators and operands
2. Traverse through the input string character by character
3. If the current character is an operator (+, -, *, /), append it to the list
4. If the current character is a digit, read the entire number and append it to the list
5. Return the list
Python3
def split_operators(input_str): operators_and_operands = [] i = 0 while i < len (input_str): if input_str[i] in ( '+' , '-' , '*' , '/' ): operators_and_operands.append(input_str[i]) i + = 1 elif input_str[i].isdigit(): start = i while i < len (input_str) and input_str[i].isdigit(): i + = 1 operators_and_operands.append(input_str[start:i]) else : i + = 1 return operators_and_operands input_str = '15+22*3-4/2' print (f "The original string is: {input_str}" ) print (f "The list after performing split functionality: {split_operators(input_str)}" ) |
The original string is: 15+22*3-4/2 The list after performing split functionality: ['15', '+', '22', '*', '3', '-', '4', '/', '2']
Time Complexity: O(n), wherer n is the length of string
Auxiliary Space: O(n), where n is length of string
Approach#5: Using reduce():
- Define the input string test_str and the operator string op.
- Apply the reduce function to the test_str input string using a lambda function as a reducer.
- The lambda function takes two arguments: s, which is the accumulated result so far, and c, which is the current character in the input string.
- If the current character c is an operator (i.e., it is in the operator string op), then use re.split function to split the accumulated result s into a list, where the delimiter is the current operator character c. The delimiter is enclosed in square brackets to capture it as a separate element in the resulting list. The re.escape function is used to escape any special characters in the operator string op to avoid errors. Finally, re.split returns a list of substrings split using the delimiter.
- If the current character c is not an operator, then the accumulated result s is returned as is.
- The reduce function returns a list of substrings and/or operators.
- Filter out any empty strings from the resulting list.
- Print the resulting list.
Python3
import re from functools import reduce test_str = "15 + 22.6 * 3-4 / 2" op = "+-*/" res = reduce ( lambda s, c: re.split( '([' + re.escape(op) + '])' , ''.join(s)) if c in op else s, test_str, test_str) res = [i for i in res if i ! = ''] print ( "The list after performing split functionality : " + str (res)) #This code is contributed by Jyothi pinjala |
The list after performing split functionality : ['15 ', '+', ' 22.6 ', '*', ' 3', '-', '4 ', '/', ' 2']
The time complexity :O(n log n), where n is the length of the input string test_str. The reduce function needs to traverse the input string test_str once, and each call to re.split has a time complexity of O(log n), where n is the length of the accumulated string s.
The space complexity : O(n), where n is the length of the input string test_str. This is because the reduce function creates a list of substrings and/or operators, and the resulting list needs to be stored in memory. The filter operation creates a new list, but this list is smaller than the original list and can be ignored for the purpose of space complexity analysis.
Approach#6: Using numpy:
Algorithm:
- Convert the input string to a numpy array of characters.
- Define a list of operators to split on.
- Use the np.isin() method to find the indices where operators occur in the array.
- Initialize a list to hold the operators and operands.
- Loop over the operator indices and extract the operands and operators, adding them to the list.
- Add the last operand to the list.
- Remove any empty strings from the list.
- Return the list of operators and operands.
Python3
import numpy as np def split_operators(input_str): # convert input string to numpy array of characters chars = np.array( list (input_str)) # define operators to split on operators = [ '+' , '-' , '*' , '/' ] # find indices where operators occur operator_indices = np.where(np.isin(chars, operators))[ 0 ] # initialize list to hold operators and operands operators_and_operands = [] # loop over operator indices to extract operators and operands start = 0 for index in operator_indices: if index > start: operand = ''.join(chars[start:index]) operators_and_operands.append(operand) operators_and_operands.append(chars[index]) start = index + 1 # add last operand to list operand = ''.join(chars[start:]) operators_and_operands.append(operand) # remove empty strings from list operators_and_operands = list ( filter ( lambda x: x ! = '', operators_and_operands)) return operators_and_operands input_str = '15+22*3-4/2' print (f "The original string is: {input_str}" ) print (f "The list after performing split functionality: {split_operators(input_str)}" ) #This code is contributed by Rayudu. |
Output:
The original string is: 15+22*3-4/2 The list after performing split functionality: ['15', '+', '22', '*', '3', '-', '4', '/', '2']
Time Complexity: The algorithm uses a single loop to extract operators and operands, so the time complexity is O(n), where n is the length of the input string.
Space Complexity: The algorithm uses a numpy array to hold the characters of the input string, so the space complexity is O(n), where n is the length of the input string. The algorithm also creates a list to hold the operators and operands, so the additional space complexity is O(k), where k is the number of operators and operands in the input string. Therefore, the total space complexity is O(n + k).