Tuesday, November 26, 2024
Google search engine
HomeLanguagesPython | Values Frequency till Maximum K

Python | Values Frequency till Maximum K

One of the problems that is basically a subproblem for many complex problems, finding numbers smaller than certain numbers in a list in Python, is commonly encountered and this particular article discusses possible solutions to this particular problem. 

Method 1 : Naive method The most common way this problem can be solved is by using a loop and just counting the occurrences of elements that are smaller than the given number K. 

Python3




# Python 3 code to demonstrate
# Values Frequency till Maximum K
# using naive method
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print ("The list : " + str(test_list))
 
# using naive method
# to get numbers < k
count = 0
for i in test_list :
    if i < k :
        count = count + 1
 
# printing the result
print ("The numbers smaller than 4 : " + str(count))


Output : 

The list : [1, 7, 5, 6, 3, 8]
The numbers smaller than 4 : 2

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

  Method 2: Using sum() The sum() can also help us achieve this task. We can return 1 when the number smaller than k is found and then compute the summation of is using sum(). 

Python3




# Python 3 code to demonstrate
# Values Frequency till Maximum K
# using sum()
 
# initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
# initializing k
k = 4
 
# printing list
print ("The list : " + str(test_list))
 
# using sum()
# to get numbers < k
count = sum(i < k for i in test_list)
 
# printing the result
print ("The numbers smaller than 4 : " + str(count))


Output : 

The list : [1, 7, 5, 6, 3, 8]
The numbers smaller than 4 : 2

Time Complexity: O(n) where n is the number of elements in the string list. The sum() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space needed

Approach 3 : Using filter()
 

Python3




#Using the filter() function, we can also achieve this task.
#Python 3 code to demonstrate
#Values Frequency till Maximum K
#using filter()
#initializing list
test_list = [1, 7, 5, 6, 3, 8]
 
#initializing k
k = 4
 
#printing list
print ("The list : " + str(test_list))
 
#using filter()
#to get numbers < k
result = filter(lambda x: x < k, test_list)
count = len(list(result))
 
#printing the result
print ("The numbers smaller than 4 : " + str(count))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The list : [1, 7, 5, 6, 3, 8]
The numbers smaller than 4 : 2

Time complexity: O(n)
Auxiliary Space: O(n)
Explanation: The filter function applies the given lambda function to each element in the list and returns the elements for which the lambda function returns True. In this case, the lambda function returns True if the element is smaller than k. The len() function is used to count the number of elements in the filtered list, which gives us the desired count of elements smaller than k in the original list.

Approach 4 : Using list comprehension:

Steps to follow :

  • Initialize a list test_list with some integers and a variable k with an integer value.
  • Use list comprehension to create a new list with elements from test_list that are less than k.
  • Use the len() function to count the number of elements in the new list.
  • Print the count of numbers smaller than k.

Python3




test_list = [1, 7, 5, 6, 3, 8]
k = 4
# printing list
print ("The list : " + str(test_list))
  
count = len([i for i in test_list if i < k])
# printing the result
print("The numbers smaller than 4: " + str(count))
#This code is contributed by Jyothi pinjala


Output

The list : [1, 7, 5, 6, 3, 8]
The numbers smaller than 4: 2

Time Complexity:  O(n), where n is the length of test_list. This is because the code iterates over each element in the list once, and list comprehension and len() functions both have a time complexity of O(n) as well.
Space Complexity: O(k), it creates a new list with at most k elements (i.e., elements smaller than k). Since k is a constant, the space complexity can be considered constant as well.

Method#5: Using the Recursive method:

Algorithm:

  1. This recursive function takes as input a list test_list and a value k and performs the following:
    1. It checks if the list is empty. If it is, it returns 0. Otherwise, it separates the first element of the list from the rest of the list. 
    2. If the first element is smaller than k, it adds 1 to the result and calls itself recursively with the rest of the list.
    3.  If the first element is not smaller than k, it calls itself recursively with the rest of the list without adding anything to the result. 
  2. The function keeps calling itself recursively until the list is empty and returns the final count.

Below is the implementation of the above approach:

Python3




# Python program for the above approach
 
# Function to count the numbers smaller
# than K
def count_smaller_than_k(test_list, k):
    if len(test_list) == 0:
        return 0
    else:
        first = test_list[0]
        rest = test_list[1:]
         
        # Recursive Calls
        if first < k:
            return 1 + count_smaller_than_k(rest, k)
        else:
            return count_smaller_than_k(rest, k)
 
 
# Driver Code
test_list = [1, 7, 5, 6, 3, 8]
k = 4
count = count_smaller_than_k(test_list, k)
 
print("The list : " + str(test_list))
 
print("The numbers smaller than {}: {}".format(k, count))


Output

The list : [1, 7, 5, 6, 3, 8]
The numbers smaller than 4: 2

Time Complexity: The time complexity of the recursive function count_smaller_than_k that I provided in my previous response is O(n), where n is the length of the input list test_list. This is because the function processes each element of the list exactly once.

Space Complexity: The space complexity of this function is also O(n) due to the recursive call stack. Each recursive call adds a new frame to the call stack, and there can be up to n recursive calls before the base case is reached. This means that the maximum size of the call stack is n.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments