Sometimes we may face a problem in which we need to find a list if it contains numbers at the Kth index with the same digits. This particular utility has an application in day-day programming. Let’s discuss certain ways in which this task can be achieved.
Method #1 : Using list comprehension + map()
We can approach this problem by converting the elements to the strings and then testing the Kth element of the string and if they are equal we can return true and then convert to set and test for the size of the result to be one. The conversion is done by map, the set function converts to set and list comprehension checks for the Kth element of the string.
Python3
# Python3 code to demonstrate# Kth Number digit match# using list comprehension + map()# initializing listtest_list = [467, 565, 2645, 8668]# printing original listprint("The original list : " + str(test_list))# initializing KK = 1# using list comprehension + map()# Kth Number digit matchres = len(set(sub[K] for sub in map(str, test_list))) == 1# Printing the resultprint("Does each element of index K have same digit ? " + str(res)) |
The original list : [467, 565, 2645, 8668] Does each element of index K have same digit ? True
Time complexity: O(n), where n is the length of the list ‘test_list’.
Auxiliary space: O(n), where n is the length of the list ‘test_list’.
Method #2 : Using all() + list comprehension
This is yet another approach in which this problem can be solved. In this we use all functions to check for all elements and return a Boolean result and list comprehension does the part of the conversion of string by str function and checking for all elements with the Kth digit of Kth element.
Python3
# Python3 code to demonstrate# Kth Number digit match# using list comprehension + all()# initializing listtest_list = [467, 565, 2645, 8668]# Printing original listprint(& quot The original list : & quot + str(test_list))# Initializing KK = 1# using list comprehension + all() method# Kth Number digit matchres = all(str(i)[K] == str(test_list[K])[K] for i in test_list)# print resultprint(& quot Does each element of index K have same digit ? & quot + str(res)) |
The original list : [467, 565, 2645, 8668] Does each element of index K have same digit ? True
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(1), constant extra space is required.
Method #3: Using for loop and len() method
Python3
# Python3 code to demonstrate# Kth Number digit match# initializing listtest_list = [467, 565, 2645, 8668]# printing original listprint("The original list : " + str(test_list))# initializing KK = 1res = Falsex = []for i in test_list: a = str(i) x.append(int(a[K]))y = [x[0]]*len(x)if(y == x): res = True# print resultprint("Does each element of index K have same digit ? " + str(res)) |
The original list : [467, 565, 2645, 8668] Does each element of index K have same digit ? True
Time Complexity: O(n), where n is the length of the input list. This is because we’re using loop + len() method which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #4 : Using reduce() and lambda
Python3
# Python3 code to demonstrate# Kth Number digit match# using reduce() and lambda# Importing reduce modulefrom functools import reduce# initializing listtest_list = [467, 565, 2645, 8668]# Printing original listprint("The original list : " + str(test_list))# Initializing KK = 1# using reduce() and lambda# Kth Number digit matchres = reduce(lambda x, y: x and (str(y)[K] == str(test_list[K])[K]), test_list)# Printing resultprint("Does each element of index K have same digit ? " + str(res))# This code is contributed by Edula Vinay Kumar Reddy |
The original list : [467, 565, 2645, 8668] Does each element of index K have same digit ? True
This method uses the reduce() function with a lambda function to check if all the Kth digits of the elements in the input list are the same. The lambda function compares the Kth digit of each element to the Kth digit of the first element in the input list.
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using Recursion
Step-by-step approach:
- Define a function `check_kth_digit()` that takes `test_list` and `K` as inputs.
- Check if the length of `test_list` is 0, if yes, return True as all elements have the same digit at index K.
- Otherwise, convert the first element of `test_list` to a string and extract the Kth digit.
- Loop through all other elements in `test_list` and compare their Kth digit with the extracted Kth digit from the first element.
- If any mismatch is found, return False as not all elements have the same digit at index K.
- If no mismatch is found, call `check_kth_digit()` recursively on the remaining elements of `test_list` (excluding the first element).
- Return the result of the recursive call.
Python3
def check_kth_digit(test_list, K): if len(test_list) == 0: # base case return True else: a = str(test_list[0]) x = int(a[K]) for i in test_list: a = str(i) if int(a[K]) != x: return False # Recursive call return True and check_kth_digit(test_list[1:], K)# Initializing listtest_list = [467, 565, 2645, 8668]# Printing original listprint("The original list : " + str(test_list))# Initializing KK = 1# Checking if each element of index K has the same digitres = check_kth_digit(test_list, K)# Printing the resultprint("Does each element of index K have the same digit? " + str(res)) |
The original list : [467, 565, 2645, 8668] Does each element of index K have the same digit? True
Time complexity: O(N*M), where N is the number of elements in `test_list` and M is the maximum number of digits in an element of `test_list`. This is because we need to loop through each element of `test_list` and extract the Kth digit, which takes O(M) time, and we do this for N elements.
Auxiliary Space: O(M), as we are storing the extracted Kth digit in a variable. Note that the space complexity could be higher if the input elements in `test_list` are very large and require a significant amount of memory for string conversion and storage of digits.
Method #6: Using a set
Step-by-step approach:
- Initialize a set named “digits” to store the digits present in the Kth position of each element of the list.
- Loop through the elements of the list.
- Convert the current element to a string and extract the Kth digit using string indexing.
- Add the extracted digit to the “digits” set.
- Check if the length of the “digits” set is 1. If yes, return True, else return False.
Python3
def check_kth_digit(test_list, K): digits = set() for num in test_list: digit = str(num)[K] digits.add(digit) return len(digits) == 1# Initializing listtest_list = [467, 565, 2645, 8668]# Printing original listprint("The original list : " + str(test_list))# Initializing KK = 1# Checking if each element of index K has the same digitres = check_kth_digit(test_list, K)# Printing the resultprint("Does each element of index K have the same digit? " + str(res)) |
The original list : [467, 565, 2645, 8668] Does each element of index K have the same digit? True
Time complexity: O(n*k), where n is the length of the list and k is the number of digits in each element.
Auxiliary space: O(k), for storing the digits in the set.
