@@ -14,12 +14,6 @@ class ListManipulator:
1414 def remove_duplicates (lst : List [Any ]) -> List [Any ]:
1515 """
1616 Removes duplicate elements from the list while maintaining the original order.
17-
18- Args:
19- lst (List[Any]): A list from which to remove duplicates.
20-
21- Returns:
22- List[Any]: A list without duplicates.
2317 """
2418 seen = set ()
2519 return [x for x in lst if not (x in seen or seen .add (x ))]
@@ -28,39 +22,20 @@ def remove_duplicates(lst: List[Any]) -> List[Any]:
2822 def flatten_nested_list (nested_list : List [List [Any ]]) -> List [Any ]:
2923 """
3024 Flattens a nested list into a single list.
31-
32- Args:
33- nested_list (List[List[Any]]): A nested list to flatten.
34-
35- Returns:
36- List[Any]: A flattened list.
3725 """
3826 return [item for sublist in nested_list for item in sublist ]
3927
4028 @staticmethod
4129 def list_intersection (lst1 : List [Any ], lst2 : List [Any ]) -> List [Any ]:
4230 """
4331 Finds the intersection of two lists.
44-
45- Args:
46- lst1 (List[Any]): The first list.
47- lst2 (List[Any]): The second list.
48-
49- Returns:
50- List[Any]: The intersection of the two lists.
5132 """
5233 return list (set (lst1 ) & set (lst2 ))
5334
5435 @staticmethod
5536 def random_shuffle (lst : List [Any ]) -> List [Any ]:
5637 """
5738 Randomly shuffles the elements of the list.
58-
59- Args:
60- lst (List[Any]): A list to shuffle.
61-
62- Returns:
63- List[Any]: A shuffled list (order will vary).
6439 """
6540 import random
6641 random .shuffle (lst )
@@ -80,6 +55,52 @@ def sort_by_frequency(lst: List[Any]) -> List[Any]:
8055 frequency = Counter (lst )
8156 return sorted (lst , key = lambda x : frequency [x ], reverse = True )
8257
58+ @staticmethod
59+ def chunk_list (lst : List [Any ], chunk_size : int ) -> List [List [Any ]]:
60+ """
61+ Splits a list into smaller chunks of a given size.
62+ """
63+ return [lst [i :i + chunk_size ] for i in range (0 , len (lst ), chunk_size )]
64+
65+ @staticmethod
66+ def most_frequent_element (lst : List [Any ]) -> Any :
67+ """
68+ Identifies the most frequently occurring element in a list.
69+ """
70+ if not lst :
71+ return None
72+ return Counter (lst ).most_common (1 )[0 ][0 ]
73+
74+ @staticmethod
75+ def rotate_list (lst : List [Any ], positions : int ) -> List [Any ]:
76+ """
77+ Rotates the list by a given number of positions.
78+ """
79+ positions %= len (lst )
80+ return lst [- positions :] + lst [:- positions ]
81+
82+ @staticmethod
83+ def unique_elements (lst : List [Any ]) -> List [Any ]:
84+ """
85+ Gets elements that appear exactly once in the list.
86+ """
87+ frequency = Counter (lst )
88+ return [x for x in lst if frequency [x ] == 1 ]
89+
90+ @staticmethod
91+ def find_pairs_with_sum (lst : List [int ], target : int ) -> List [tuple ]:
92+ """
93+ Finds all pairs of numbers that sum to a specific target.
94+ """
95+ seen = set ()
96+ pairs = []
97+ for num in lst :
98+ complement = target - num
99+ if complement in seen :
100+ pairs .append ((complement , num ))
101+ seen .add (num )
102+ return pairs
103+
83104# Example usage of the functions in the script
84105if __name__ == "__main__" :
85106 sample_list = [1 , 2 , 2 , 3 , 4 , 4 , 5 , 5 , 5 ]
@@ -91,4 +112,8 @@ def sort_by_frequency(lst: List[Any]) -> List[Any]:
91112 logging .info ("List Intersection: %s" , ListManipulator .list_intersection ([1 , 2 , 3 ], [2 , 3 , 4 ]))
92113 logging .info ("Shuffled List: %s" , ListManipulator .random_shuffle (sample_list .copy ())) # Using copy to keep original
93114 logging .info ("Sorted by Frequency: %s" , ListManipulator .sort_by_frequency (sample_list ))
94-
115+ logging .info ("Chunked List: %s" , ListManipulator .chunk_list (sample_list , 2 ))
116+ logging .info ("Most Frequent Element: %s" , ListManipulator .most_frequent_element (sample_list ))
117+ logging .info ("Rotated List: %s" , ListManipulator .rotate_list (sample_list , 2 ))
118+ logging .info ("Unique Elements: %s" , ListManipulator .unique_elements (sample_list ))
119+ logging .info ("Pairs with Sum 6: %s" , ListManipulator .find_pairs_with_sum (sample_list , 6 ))
0 commit comments