Python – Elements with factors count less than K
Last Updated :
08 Mar, 2023
Given a List of elements, get all elements with factors less than K.
Examples:
Input : test_list = [60, 12, 100, 17, 18, 19]. K = 4
Output : [17, 19]
Explanation : Both elements have 2 factors.
Input : test_list = [60, 12, 100, 360, 18, 900]. K = 4
Output : []
Explanation : All have greater than 4 factors.
Method #1: Using list comprehension
In this, we use list comprehension to get factors count, and other list comprehension to iterate for all the elements in list.
Python3
def factors_less_k(ele, K):
return len ([idx for idx in range ( 1 , ele + 1 ) if ele % idx = = 0 ]) < = K
test_list = [ 60 , 12 , 100 , 17 , 18 ]
print ( "The original list is : " + str (test_list))
K = 4
res = [ele for ele in test_list if factors_less_k(ele, K)]
print ( "Filtered elements : " + str (res))
|
Output
The original list is : [60, 12, 100, 17, 18]
Filtered elements : [17]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + len() + list comprehension
In this, task of filtering is done using filter() and lambda, len(), and list comprehension is used to perform tasks of getting factors.
Python3
def factors_less_k(ele, K):
return len ([idx for idx in range ( 1 , ele + 1 ) if ele % idx = = 0 ]) < = K
test_list = [ 60 , 12 , 100 , 17 , 18 ]
print ( "The original list is : " + str (test_list))
K = 4
res = list ( filter ( lambda ele: factors_less_k(ele, K), test_list))
print ( "Filtered elements : " + str (res))
|
Output
The original list is : [60, 12, 100, 17, 18]
Filtered elements : [17]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. This is because we’re using the built-in filter() + lambda + len() + list comprehension which all has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), no extra space is required
Method #3 : Using a for loop :
Algorithm:
1.Initialize an empty list res to store the filtered elements.
2.Iterate through each element ele in the input list test_list.
3.For each element, initialize a variable factors to 0.
4.Iterate through all numbers from 1 to ele and check if ele is divisible by the number.
5.If ele is divisible by the number, increment factors.
6.Check if the number of factors factors is less than or equal to K.
7.If factors is less than or equal to K, append ele to the res list.
8.Return the res list containing the filtered elements.
Python3
test_list = [ 60 , 12 , 100 , 17 , 18 ]
K = 4
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
factors = 0
for i in range ( 1 , ele + 1 ):
if ele % i = = 0 :
factors + = 1
if factors < = K:
res.append(ele)
print ( "Filtered elements : " + str (res))
|
Output
The original list is : [60, 12, 100, 17, 18]
Filtered elements : [17]
Time Complexity: O(n^2), where n is the length of the input list. This is because for each element in the list, we need to iterate through all numbers up to the element itself to count its factors. The outer loop runs n times, and the inner loop runs up to n times for the largest element in the list.
Auxiliary Space: O(1), because we are not using any additional data structures that scale with the size of the input. The only extra space used is for the res list, which has a maximum size of n, the length of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...