Python – Check if Elements delimited by K
Given a String, check if each segment is delimited by K.
Input : test_str = ’72!45!geeks!best’, K = ‘!’
Output : True
Explanation : All numerics and alphabets separated by delim border.Input : test_str = ’72!45geeks!best’, K = ‘!’
Output : False
Explanation : No separation between 45 and geeks.
Method 1: Using isdigit() + isalpha() + loop
This is way in which this task can be performed. In this, we perform task of checking for alphabets and digits segments using isalpha() and isdigit(). Presence of any element, not entirely number or alphabet, is termed to non-delimited by K, and it remains unresolved during split().
Python3
# Python3 code to demonstrate working of # Check if Elements delimited by K # Using isdigit() + isalpha() + loop # initializing string test_str = '72@45@geeks@best' # printing original string print ( "The original string is : " + str (test_str)) # initializing splt_chr K = "@" res = True # splitting elements temp = test_str.split(K) for ele in temp: # checking for non-alpha or non-digit if len (ele) > 1 and not ele.isdigit() and not ele.isalpha(): res = False break # printing result print ( "Are all delimited by K : " + str (res)) |
The original string is : 72@45@geeks@best Are all delimited by K : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using the re module to split the string and check for elements
Step-by-step algorithm:
- Split the given string using the delimiter K using re.split() method and store in a temporary variable ‘temp’.
- Check if each element in ‘temp’ is either a single character or consists of only alphabets or only digits using all() method.
- Print the final result.
Python3
import re # initializing string test_str = '72@45@geeks@best' # initializing splt_chr K = "@" # printing original string print ( "The original string is : " + str (test_str)) temp = re.split(re.escape(K), test_str) res = all ( len (ele) = = 1 or ele.isdigit() or ele.isalpha() for ele in temp) # printing result print ( "Are all delimited by K : " + str (res)) |
The original string is : 72@45@geeks@best Are all delimited by K : True
Time Complexity: O(n), where n is the length of the given string test_str since we need to iterate over each element in ‘temp’.
Auxiliary Space: O(n), since we are storing the split string in a list ‘temp’ of size n.
Method #3: Using all() and generator expression
- Split the string test_str based on the delimiter K using the split() method and generate a list of substrings.
- Check if each element of the list generated in the previous step is valid by iterating over it and applying the following conditions:
- len(ele) == 1 checks if the element has a length of 1.
- ele.isalpha() checks if the element is an alphabet.
- ele.isdigit() checks if the element is a digit.
- Use the all() method to check if all the elements of the list satisfy the above conditions and assign the result to a variable res.
- Print the result of whether all elements are delimited by the specified delimiter or not using the print() statement.
Python3
# initializing string test_str = '72@45@geeks@best' # initializing splt_chr K = "@" # printing original string print ( "The original string is : " + str (test_str)) # split the string based on delimiter # and check if each element is valid res = all ( len (ele) = = 1 or ele.isalpha() or ele.isdigit() for ele in test_str.split(K)) # print the result print ( "Are all delimited by K: " + str (res)) |
The original string is : 72@45@geeks@best Are all delimited by K: True
Time Complexity: O(n), where n is the length of the given string test_str since we need to iterate over each element in ‘temp’.
Auxiliary Space: O(n), since we are storing the split string in a list ‘
Method #4: Manual Splitting and Validation Method
- Initialize a boolean variable valid to True.
- Initialize an empty string current to store the current sub-string.
- Loop through each character in the input string.
- If the current character is the delimiter character K, check if the current sub-string is valid by either checking its length or using the isalpha() and isdigit() methods. If it’s not valid, set the valid variable to False.
- If the current character is not the delimiter character K, append it to the current sub-string.
- After the loop is done, check the validity of the last sub-string by checking its length or using the isalpha() and isdigit() methods. If it’s not valid, set the valid variable to False.
- Print the value of the valid variable
Python3
# initializing string test_str = '72@45@geeks@best' # initializing splt_chr K = "@" # printing original string print ( "The original string is : " + str (test_str)) # initialize variables valid = True current = "" # loop through each character in the input string for c in test_str: if c = = K: # if delimiter if len (current) = = 0 or not (current.isalpha() or current.isdigit()): valid = False break current = "" else : # if not delimiter current + = c # check last sub-string if len (current) = = 0 or not (current.isalpha() or current.isdigit()): valid = False # print the result print ( "Are all delimited by K: " + str (valid)) |
The original string is : 72@45@geeks@best Are all delimited by K: True
Time complexity: O(n) where n is the length of the input string.
Auxiliary space: O(1) as we are using constant amount of space for storing variables.
Method #5: Using the split() method and the isalnum() method:
Python3
# initializing string test_str = '72@45@geeks@best' # initializing delimiter delimiter = '@' # printing original string print ( "The original string is : " + str (test_str)) # split the string using the delimiter split_str = test_str.split(delimiter) # initialize variables valid = True # loop through each substring and check if it contains only alphanumeric characters for s in split_str: if not s.isalnum(): valid = False break # print the result print ( "Are all delimited by K: " + str (valid)) |
The original string is : 72@45@geeks@best Are all delimited by K: True
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1) as we are using constant amount of space for storing variables.
Please Login to comment...