Given a List of elements, extract all elements which have digits that are increasing in order.
Input : test_list = [1234, 7373, 3643, 3527, 148, 49] Output : [1234, 148, 49] Explanation : All elements have increasing digits.
Input : test_list = [12341, 7373, 3643, 3527, 1481, 491] Output : [] Explanation : No elements have all increasing digits.
Method 1: Using loop and str()
In this, we convert each element to a string and then check if each digit is greater than the previous one using a loop.
Python3
# initializing list test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ] # printing original list print ( "The original list is : " + str (test_list)) # loop to check for each element res = [] for ele in test_list: flag = True for idx in range ( len ( str (ele)) - 1 ): # checking for each next digit if str (ele)[idx + 1 ] < = str (ele)[idx]: flag = False if flag: res.append(ele) # printing result print ( "Extracted increasing digits : " + str (res)) |
Output:
The original list is : [1234, 7373, 3643, 3527, 148] Extracted increasing digits : [1234, 148]
Time complexity: O(n*m) where n is the length of the list and m is the maximum number of digits in a number in the list.
Auxiliary space: O(k) where k is the number of increasing digit numbers in the list.
Method 2 : Using sorted(), list comprehension and str()
In this, we test for each digit of an element to be increasing by sorting each element and comparing it with original version. If they are same, the element is added to desired list.
Python3
# initializing list test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ] # printing original list print ( "The original list is : " + str (test_list)) # sorting and comparing for equality res = [ele for ele in test_list if ''.join( sorted ( str (ele))) = = str (ele)] # printing result print ( "Extracted increasing digits : " + str (res)) |
Output:
The original list is : [1234, 7373, 3643, 3527, 148] Extracted increasing digits : [1234, 148]
Time complexity: O(nlogn), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list.
Method 3 : Using map() function and lambda
- Initialize the input list
- Define a lambda function to extract the increasing digits in each element of the list.
- Use the map() function along with the lambda function to extract the increasing digits in each element of the list.
- Print the result.
Python3
# initialize input list test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ] # define lambda function to extract increasing digits in each element extract_inc_digits = lambda x: all ( str (x)[i] < str (x)[i + 1 ] for i in range ( len ( str (x)) - 1 )) # use map() and filter() to extract increasing digits in each element res = list ( filter (extract_inc_digits, test_list)) # print result print ( "Extracted increasing digits : " + str (res)) |
Extracted increasing digits : [1234, 148]
Time complexity: O(nk), where n is the length of the input list and k is the maximum number of digits in an element of the list.
Auxiliary space: O(k), since we are only storing the digits of each element in memory during the execution of the lambda function.
Method 4: Using the built-in filter() function
Steps:
- We define a function is_increasing() which takes a number as input and returns True if the digits of the number are in increasing order, and False otherwise.
- We use the filter() function to apply this function to each element of the test_list and return only the elements for which the function returns True.
- We convert the resulting filter object to a list using list() function.
- We print the final result.
Python3
# initializing list test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ] # printing original list print ( "The original list is : " + str (test_list)) # function to check if digits are in increasing order def is_increasing(num): digits = list ( str (num)) return all ( int (digits[i]) < int (digits[i + 1 ]) for i in range ( len (digits) - 1 )) # Extracting elements with increasing digits # using filter() method res = list ( filter (is_increasing, test_list)) # printing result print ( "Extracted increasing digits : " + str (res)) |
The original list is : [1234, 7373, 3643, 3527, 148] Extracted increasing digits : [1234, 148]
Time complexity: O(n * m), where n is the length of the test_list and m is the maximum number of digits in a number in the list.
Auxiliary space: O(m), where m is the maximum number of digits in a number in the list.
Method 5: Using recursion
Step-by-step approach:
- Define a function that takes a number as input.
- Convert the number to a string and check if its digits are in increasing order using a string manipulation technique.
- If the digits are not in increasing order, return False.
- If the number has only one digit or the digits are in increasing order, return True.
- Otherwise, recursively call the function on the number without its first digit.
- If the recursive call returns True and the first digit is less than the second digit, return True.
- Otherwise, return False.
- Initialize an empty list to store the result.
- Loop through each number in the test_list and call the function on it.
- If the function returns True, append the number to the result list.
- Return the result list.
Python3
# initializing list test_list = [ 1234 , 7373 , 3643 , 3527 , 148 ] # printing original list print ( "The original list is : " + str (test_list)) # function to check if digits are in increasing order using recursion def is_increasing(num): str_num = str (num) if len (str_num) = = 1 : return True elif str_num[ 0 ] > = str_num[ 1 ]: return False else : return is_increasing( int (str_num[ 1 :])) # Extracting elements with increasing digits using recursion res = [num for num in test_list if is_increasing(num)] # printing result print ( "Extracted increasing digits : " + str (res)) |
The original list is : [1234, 7373, 3643, 3527, 148] Extracted increasing digits : [1234, 148]
The time complexity of this method is O(n log n), where n is the length of the test_list.
The auxiliary space complexity of this method is O(log n), which is the maximum depth of the recursion.