Python – Match Kth number digit in list elements
Last Updated :
10 May, 2023
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
test_list = [ 467 , 565 , 2645 , 8668 ]
print ( "The original list : " + str (test_list))
K = 1
res = len ( set (sub[K] for sub in map ( str , test_list))) = = 1
print ( "Does each element of index K have same digit ? " + str (res))
|
Output :
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
test_list = [ 467 , 565 , 2645 , 8668 ]
print (& quot
The original list : & quot
+ str (test_list))
K = 1
res = all ( str (i)[K] = = str (test_list[K])[K] for i in test_list)
print (& quot
Does each element of index K have same digit ? & quot
+ str (res))
|
Output :
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
test_list = [ 467 , 565 , 2645 , 8668 ]
print ( "The original list : " + str (test_list))
K = 1
res = False
x = []
for i in test_list:
a = str (i)
x.append( int (a[K]))
y = [x[ 0 ]] * len (x)
if (y = = x):
res = True
print ( "Does each element of index K have same digit ? " + str (res))
|
Output
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
from functools import reduce
test_list = [ 467 , 565 , 2645 , 8668 ]
print ( "The original list : " + str (test_list))
K = 1
res = reduce ( lambda x, y: x and ( str (y)[K] = = str (test_list[K])[K]), test_list)
print ( "Does each element of index K have same digit ? " + str (res))
|
Output
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 :
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
return True and check_kth_digit(test_list[ 1 :], K)
test_list = [ 467 , 565 , 2645 , 8668 ]
print ( "The original list : " + str (test_list))
K = 1
res = check_kth_digit(test_list, K)
print ( "Does each element of index K have the same digit? " + str (res))
|
Output
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
test_list = [ 467 , 565 , 2645 , 8668 ]
print ( "The original list : " + str (test_list))
K = 1
res = check_kth_digit(test_list, K)
print ( "Does each element of index K have the same digit? " + str (res))
|
Output
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...