Python – Elements with factors count less than K

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
# Python3 code to demonstrate working of# Elements with factors less than K# Using list comprehensiondef factors_less_k(ele, K): # comparing for factors return len([idx for idx in range(1, ele + 1) if ele % idx == 0]) <= K# initializing listtest_list = [60, 12, 100, 17, 18]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 4# checking for each elementres = [ele for ele in test_list if factors_less_k(ele, K)]# printing resultprint("Filtered elements : " + str(res)) |
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
# Python3 code to demonstrate working of# Elements with factors less than K# Using filter() + lambda + len() + list comprehensiondef factors_less_k(ele, K): # comparing for factors return len([idx for idx in range(1, ele + 1) if ele % idx == 0]) <= K# initializing listtest_list = [60, 12, 100, 17, 18]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 4# filtering using filter() + lambdares = list(filter(lambda ele: factors_less_k(ele, K), test_list))# printing resultprint("Filtered elements : " + str(res)) |
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
# Initializing listtest_list = [60, 12, 100, 17, 18]# Initializing KK = 4# printing original listprint("The original list is : " + str(test_list))# Empty list to store filtered elementsres = []# Iterating through the listfor ele in test_list: # Counting factors of the element factors = 0 for i in range(1, ele+1): if ele % i == 0: factors += 1 # Checking if the number of factors is less than or equal to K if factors <= K: res.append(ele)# Printing resultprint("Filtered elements : " + str(res))#This code is contributed by Jyothi pinjala. |
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.



