Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | K Division Grouping

Python | K Division Grouping

Sometimes, we have a problem in which we need to deal with the grouping of elements. These groupings are comparatively easier while working with the databases, but using languages, this can be tricky. Let’s discuss certain ways to perform the grouping in Python by K.

 Method #1 : Using loops This is the brute force method to perform this particular task. In this we use loops to have each number’s K’s place and add that number to designated list. 

Python3




# Python3 code to demonstrate
# K Division Grouping
# using loops
 
# initializing list
test_list = [3, 12, 13, 22, 25, 30]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing K
K = 7
 
# using loops
# K Division Grouping
res = []
dec = -1
for num in sorted(test_list):
    while num // K != dec:
        res.append([])
        dec += 1
    res[-1].append(num)
 
# print result
print("The list after grouping by K is : " + str(res))


Output : 

The original list : [3, 12, 13, 22, 25, 30]
The list after grouping by K is : [[3], [12, 13], [], [22, 25], [30]]

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

  Method #2 : Using list comprehension + max() + min() Another method to perform this particular task in a shortened way is by using list comprehension. The min and max function specify the number of lists required as inner lists and rest of the task is performed inside the list comprehension. 

Python3




test_list = [3, 12, 13, 22, 25, 30]
K = 7
 
res = []
dec = -1
for num in sorted(test_list):
    while num // K != dec:
        res.append([])
        dec += 1
    res[-1].append(num)
 
print("The list after grouping by K is : " + str(res))


Output : 

The original list : [3, 12, 13, 22, 25, 30]
The list after grouping by K is : [[3], [12, 13], [], [22, 25], [30]]

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

Method #3: Using list comprehension + sorted() + range()

Step-by-step Algorithm :

  1. Initialize an empty list res of sublists, where the number of sublists is (max(test_list) // K) + 1.
  2. Iterate over the sorted elements of test_list:
    a) Compute the floor division of the current element by K.
    b) Append the current element to the sublist in res corresponding to the computed index.
  3. Return the res list containing the grouped sublists.

Python3




test_list = [3, 12, 13, 22, 25, 30]
K = 7
print("The original list before grouping by K is : ", str(test_list))
# Using list comprehension
res = [[] for _ in range((max(test_list) // K) + 1)]
for num in sorted(test_list):
    res[num // K].append(num)
 
# print result
print("The list after grouping by K is : ", res)


Output

The original list before grouping by K is :  [3, 12, 13, 22, 25, 30]
The list after grouping by K is :  [[3], [12, 13], [], [22, 25], [30]]

Complexity Analysis :

Time Complexity: O(n logn)
This is due to the sorting step. The iteration over the sorted elements takes linear time, which is dominated by the sorting time.

Space Complexity: O(m)
The space complexity of this algorithm is O(m), where m is the number of sublists in res. The number of sublists is (max(test_list) // K) + 1, which is proportional to the range of values in test_list. The space used by res is therefore proportional to the range of values in test_list. The space used by the other variables and temporary data structures is negligible.

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