-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from yashksaini-coder/yash/fix-348
perf: ⚡️ Add logging, dataclasses + test cases + improved README
- Loading branch information
Showing
4 changed files
with
172 additions
and
185 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,84 @@ | ||
List Manipulation Module body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; } h1, h2, h3 { color: #333; } code { background-color: #f4f4f4; padding: 5px; border-radius: 3px; } pre { background-color: #f4f4f4; padding: 10px; border-radius: 5px; overflow-x: auto; } hr { margin: 20px 0; } | ||
# List Manipulation Module | ||
|
||
List Manipulation Module | ||
======================== | ||
## Introduction | ||
|
||
Introduction | ||
------------ | ||
The **List Manipulation Module** is a collection of essential functions designed to facilitate efficient data handling and analysis of lists in Python. Each function is crafted to simplify common tasks related to list operations, making it easier for developers to manipulate and analyze list data. | ||
|
||
The **List Manipulation Module** provides essential functions for working with lists, enabling efficient data handling and analysis. Each function is designed to facilitate common tasks related to list operations. | ||
## Functionality Overview | ||
|
||
Functionality | ||
------------- | ||
This module includes several key functions that perform various operations on lists. Below is a detailed description of each function, including its purpose, parameters, and return values. | ||
|
||
### Remove Duplicates | ||
### 1. Remove Duplicates | ||
|
||
This function removes duplicate elements from the list while maintaining the original order. | ||
This function removes duplicate elements from a list while maintaining the original order of the elements. | ||
|
||
remove_duplicates(lst) | ||
```python | ||
remove_duplicates(lst: List[Any]) -> List[Any] | ||
``` | ||
|
||
* **Args**: | ||
* `lst` (list): A list from which to remove duplicates. | ||
* **Returns**: | ||
* `list`: A list without duplicates. | ||
```python | ||
remove_duplicates([1, 2, 2, 3, 4, 4]) # Returns: [1, 2, 3, 4] | ||
``` | ||
|
||
**Example**: | ||
### 2. Flatten Nested List | ||
|
||
>>> remove_duplicates([1, 2, 2, 3, 4, 4]) | ||
[1, 2, 3, 4] | ||
This function flattens a nested list structure into a single list. | ||
|
||
* * * | ||
```python | ||
flatten_nested_list(nested_list: List[List[Any]]) -> List[Any] | ||
``` | ||
|
||
### Flatten Nested List | ||
```python | ||
flatten_nested_list([[1, 2], [3, 4], [5]]) # Returns: [1, 2, 3, 4, 5] | ||
``` | ||
|
||
This function flattens a nested list into a single list. | ||
### 3. List Intersection | ||
|
||
flatten_nested_list(nested_list) | ||
This function returns a list of elements that are common to both input lists. | ||
|
||
* **Args**: | ||
* `nested_list` (list): A nested list to flatten. | ||
* **Returns**: | ||
* `list`: A flattened list. | ||
```python | ||
list_intersection(lst1: List[Any], lst2: List[Any]) -> List[Any] | ||
``` | ||
|
||
**Example**: | ||
```python | ||
list_intersection([1, 2, 3], [2, 3, 4]) # Returns: [2, 3] | ||
``` | ||
|
||
>>> flatten_nested_list([[1, 2], [3, 4], [5]]) | ||
[1, 2, 3, 4, 5] | ||
### 4. Random Shuffle | ||
|
||
* * * | ||
This function shuffles the elements of a list randomly. | ||
|
||
### List Intersection | ||
```python | ||
random_shuffle([1, 2, 3, 4]) # Returns: A shuffled version of the list | ||
``` | ||
|
||
This function finds the intersection of two lists. | ||
### 5. Sort by Frequency | ||
|
||
list_intersection(lst1, lst2) | ||
This function sorts the elements of a list by their frequency in descending order. | ||
|
||
* **Args**: | ||
* `lst1` (list): The first list. | ||
* `lst2` (list): The second list. | ||
* **Returns**: | ||
* `list`: The intersection of the two lists. | ||
```python | ||
sort_by_frequency(lst: List[Any]) -> List[Any] | ||
sort_by_frequency([4, 5, 6, 4, 5, 4]) # Returns: [4, 4, 4, 5, 5, 6] | ||
``` | ||
|
||
**Example**: | ||
## Summary of Enhancements | ||
|
||
>>> list_intersection([1, 2, 3], [2, 3, 4]) | ||
[2, 3] | ||
Added detailed descriptions for each function, including parameters and return values. | ||
Provided example usage for clarity. | ||
Explained the approach and principles behind the module's design. | ||
Let me know if you need any further modifications or additional information! | ||
|
||
* * * | ||
```python | ||
print("Without Duplicates:", ListManipulator.remove_duplicates(sample_list)) | ||
print("Flattened Nested List:", ListManipulator.flatten_nested_list([[1, 2], [3, 4], [5]])) | ||
print("List Intersection:", ListManipulator.list_intersection([1, 2, 3], [2, 3, 4])) | ||
print("Shuffled List:", ListManipulator.random_shuffle(sample_list.copy())) | ||
print("Sorted by Frequency:", ListManipulator.sort_by_frequency(sample_list)) | ||
``` | ||
|
||
### Random Shuffle | ||
|
||
This function randomly shuffles the elements of the list. | ||
|
||
random_shuffle(lst) | ||
|
||
* **Args**: | ||
* `lst` (list): A list to shuffle. | ||
* **Returns**: | ||
* `list`: A shuffled list (order will vary). | ||
|
||
**Example**: | ||
|
||
>>> random_shuffle([1, 2, 3, 4]) | ||
[3, 1, 4, 2] # (order will vary) | ||
|
||
* * * | ||
|
||
### Sort by Frequency | ||
|
||
This function sorts the list based on the frequency of elements in descending order. | ||
|
||
sort_by_frequency(lst) | ||
|
||
* **Args**: | ||
* `lst` (list): A list to sort by frequency. | ||
* **Returns**: | ||
* `list`: The sorted list by frequency. | ||
|
||
**Example**: | ||
|
||
>>> sort_by_frequency([4, 5, 6, 4, 5, 4]) | ||
[4, 4, 4, 5, 5, 6] | ||
|
||
* * * | ||
|
||
Feel free to reach out if you have any questions about how to use the List Manipulation Module! | ||
## Summary of Enhancements | ||
|
||
- Added detailed descriptions for each function, including parameters and return values. | ||
- Provided example usage for clarity. | ||
- Explained the approach and principles behind the module's design. | ||
- Let me know if you need any further modifications or additional information! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,94 @@ | ||
def remove_duplicates(lst: list) -> list: | ||
""" | ||
Removes duplicate elements from the list while maintaining the original order. | ||
Example usage: | ||
remove_duplicates([1, 2, 2, 3, 4, 4]) -> [1, 2, 3, 4] | ||
""" | ||
seen = set() | ||
return [x for x in lst if not (x in seen or seen.add(x))] | ||
|
||
def flatten_nested_list(nested_list: list) -> list: | ||
""" | ||
Flattens a nested list into a single list. | ||
Example usage: | ||
flatten_nested_list([[1, 2], [3, 4], [5]]) -> [1, 2, 3, 4, 5] | ||
""" | ||
return [item for sublist in nested_list for item in sublist] | ||
|
||
def list_intersection(lst1: list, lst2: list) -> list: | ||
""" | ||
Finds the intersection of two lists. | ||
Example usage: | ||
list_intersection([1, 2, 3], [2, 3, 4]) -> [2, 3] | ||
""" | ||
return list(set(lst1) & set(lst2)) | ||
|
||
def random_shuffle(lst: list) -> list: | ||
""" | ||
Randomly shuffles the elements of the list. | ||
Example usage: | ||
random_shuffle([1, 2, 3, 4]) -> [3, 1, 4, 2] (order will vary) | ||
""" | ||
import random | ||
random.shuffle(lst) | ||
return lst | ||
|
||
def sort_by_frequency(lst: list) -> list: | ||
""" | ||
Sorts the list based on the frequency of elements in descending order. | ||
Example usage: | ||
sort_by_frequency([4, 5, 6, 4, 5, 4]) -> [4, 4, 4, 5, 5, 6] | ||
""" | ||
from collections import Counter | ||
frequency = Counter(lst) | ||
return sorted(lst, key=lambda x: frequency[x], reverse=True) | ||
import logging | ||
from collections import Counter | ||
from dataclasses import dataclass | ||
from typing import List, Any | ||
|
||
# Configure logging | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
@dataclass | ||
class ListManipulator: | ||
"""Class for manipulating lists with various operations.""" | ||
|
||
@staticmethod | ||
def remove_duplicates(lst: List[Any]) -> List[Any]: | ||
""" | ||
Removes duplicate elements from the list while maintaining the original order. | ||
Args: | ||
lst (List[Any]): A list from which to remove duplicates. | ||
Returns: | ||
List[Any]: A list without duplicates. | ||
""" | ||
seen = set() | ||
return [x for x in lst if not (x in seen or seen.add(x))] | ||
|
||
@staticmethod | ||
def flatten_nested_list(nested_list: List[List[Any]]) -> List[Any]: | ||
""" | ||
Flattens a nested list into a single list. | ||
Args: | ||
nested_list (List[List[Any]]): A nested list to flatten. | ||
Returns: | ||
List[Any]: A flattened list. | ||
""" | ||
return [item for sublist in nested_list for item in sublist] | ||
|
||
@staticmethod | ||
def list_intersection(lst1: List[Any], lst2: List[Any]) -> List[Any]: | ||
""" | ||
Finds the intersection of two lists. | ||
Args: | ||
lst1 (List[Any]): The first list. | ||
lst2 (List[Any]): The second list. | ||
Returns: | ||
List[Any]: The intersection of the two lists. | ||
""" | ||
return list(set(lst1) & set(lst2)) | ||
|
||
@staticmethod | ||
def random_shuffle(lst: List[Any]) -> List[Any]: | ||
""" | ||
Randomly shuffles the elements of the list. | ||
Args: | ||
lst (List[Any]): A list to shuffle. | ||
Returns: | ||
List[Any]: A shuffled list (order will vary). | ||
""" | ||
import random | ||
random.shuffle(lst) | ||
return lst | ||
|
||
@staticmethod | ||
def sort_by_frequency(lst: List[Any]) -> List[Any]: | ||
""" | ||
Sorts the list based on the frequency of elements in descending order. | ||
Args: | ||
lst (List[Any]): A list to sort by frequency. | ||
Returns: | ||
List[Any]: The sorted list by frequency. | ||
""" | ||
frequency = Counter(lst) | ||
return sorted(lst, key=lambda x: frequency[x], reverse=True) | ||
|
||
# Example usage of the functions in the script | ||
if __name__ == "__main__": | ||
sample_list = [1, 2, 2, 3, 4, 4, 5, 5, 5] | ||
nested_list = [[1, 2], [3, 4], [5]] | ||
|
||
print("Original List:", sample_list) | ||
print("Without Duplicates:", remove_duplicates(sample_list)) | ||
print("Flattened Nested List:", flatten_nested_list(nested_list)) | ||
print("List Intersection:", list_intersection([1, 2, 3], [2, 3, 4])) | ||
print("Shuffled List:", random_shuffle(sample_list.copy())) # Using copy to keep original | ||
print("Sorted by Frequency:", sort_by_frequency(sample_list)) | ||
logging.info("Original List: %s", sample_list) | ||
logging.info("Without Duplicates: %s", ListManipulator.remove_duplicates(sample_list)) | ||
logging.info("Flattened Nested List: %s", ListManipulator.flatten_nested_list(nested_list)) | ||
logging.info("List Intersection: %s", ListManipulator.list_intersection([1, 2, 3], [2, 3, 4])) | ||
logging.info("Shuffled List: %s", ListManipulator.random_shuffle(sample_list.copy())) # Using copy to keep original | ||
logging.info("Sorted by Frequency: %s", ListManipulator.sort_by_frequency(sample_list)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import unittest | ||
from list_manipulation import ListManipulator | ||
|
||
class TestListManipulator(unittest.TestCase): | ||
|
||
def test_remove_duplicates(self): | ||
self.assertEqual(ListManipulator.remove_duplicates([1, 2, 2, 3, 4, 4]), [1, 2, 3, 4]) | ||
self.assertEqual(ListManipulator.remove_duplicates([1, 1, 1]), [1]) | ||
|
||
def test_flatten_nested_list(self): | ||
self.assertEqual(ListManipulator.flatten_nested_list([[1, 2], [3, 4], [5]]), [1, 2, 3, 4, 5]) | ||
self.assertEqual(ListManipulator.flatten_nested_list([]), []) | ||
|
||
def test_list_intersection(self): | ||
self.assertEqual(ListManipulator.list_intersection([1, 2, 3], [2, 3, 4]), [2, 3]) | ||
self.assertEqual(ListManipulator.list_intersection([1, 2], [3, 4]), []) | ||
|
||
def test_random_shuffle(self): | ||
shuffled = ListManipulator.random_shuffle([1, 2, 3, 4]) | ||
self.assertEqual(sorted(shuffled), [1, 2, 3, 4]) # Check if all elements are present | ||
|
||
def test_sort_by_frequency(self): | ||
self.assertEqual(ListManipulator.sort_by_frequency([4, 5, 6, 4, 5, 4]), [4, 4, 4, 5, 5, 6]) | ||
self.assertEqual(ListManipulator.sort_by_frequency([1, 2, 2, 3, 3, 3]), [3, 3, 3, 2, 2, 1]) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |