Python – Test if all digits starts from % K digit
Sometimes we may face a problem in which we need to find for a list if it contains numbers which are % K. 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 starting element of string and if they are % K we can return true and then convert to set and test for size of result to be one. The conversion is done by map, set function converts to set and list comprehension checks for first element of string.
Python3
test_list = [ 65 , 3 , 92 , 332 ]
print ("The original list : " + str (test_list))
K = 3
res = len ( set ( not ( int (sub[ 0 ]) % K) for sub in map ( str , test_list))) = = 1
print ("Does each element start with % K digit ? " + str (res))
|
Output :
The original list : [65, 3, 92, 332]
Does each element start with % K digit ? True
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n), since the map() function creates a new list of length n containing the string representation of each integer in 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 function to check for all elements and return a Boolean result and list comprehension does the part of conversion of string by str function and checking for all elements with the first digit of first element to be % K.
Python3
test_list = [ 65 , 3 , 92 , 332 ]
print ("The original list : " + str (test_list))
K = 3
res = not all ( int ( str (i)[ 0 ]) % K for i in test_list)
print ("Does each element start with % K digit ? " + str (res))
|
Output :
The original list : [65, 3, 92, 332]
Does each element start with % K digit ? True
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the space used does not depend on the input size.
Method #3 : Using reduce()
This approach uses the reduce() function to iterate over the list and check if the first digit of each element is a multiple of K. The lambda function passed to reduce() takes two arguments, x and y, where x is the cumulative result of the previous iteration and y is the current element being processed. The lambda function returns True if the first digit of y is a multiple of K, and False otherwise. The reduce() function returns the cumulative result, which is a Boolean indicating if all elements in the list start with a digit that is a multiple of K.
Python3
from functools import reduce
def check_if_starts_with_k(lst, k):
return reduce ( lambda x, y: x and int ( str (y)[ 0 ]) % k = = 0 , lst, True )
test_list = [ 65 , 3 , 92 , 332 ]
K = 3
result = check_if_starts_with_k(test_list, K)
print ( "Does each element start with % K digit? " , result)
|
Output
Does each element start with % K digit? True
Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1), as the function uses a single variable to store the cumulative result.
Method #4 : Using for loop
Python3
test_list = [ 65 , 3 , 92 , 332 ]
print ( "The original list : " + str (test_list))
K = 3
c = 0
res = False
for i in test_list:
x = str (i)
if ( int (x[ 0 ]) % K = = 0 ):
c + = 1
if (c = = len (test_list)):
res = True
print ( "Does each element start with % K digit ? " + str (res))
|
Output
The original list : [65, 3, 92, 332]
Does each element start with % K digit ? True
Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(1)
Last Updated :
18 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...