-
Notifications
You must be signed in to change notification settings - Fork 75
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 #81 from mehul-m-prajapati/list-manip
Added list manipulation methods
- Loading branch information
Showing
4 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
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,51 @@ | ||
# test_list_manipulation.py | ||
import unittest | ||
from pysnippets.lists.list_manipulation import ( | ||
add_to_list, | ||
remove_from_list, | ||
find_in_list, | ||
sort_list, | ||
merge_lists, | ||
unique_elements, | ||
list_average, | ||
) | ||
|
||
class TestListManipulation(unittest.TestCase): | ||
|
||
def test_add_to_list(self): | ||
lst = [1, 2, 3] | ||
self.assertEqual(add_to_list(lst, 4), [1, 2, 3, 4]) | ||
self.assertEqual(add_to_list([], 1), [1]) | ||
|
||
def test_remove_from_list(self): | ||
lst = [1, 2, 3, 4] | ||
self.assertEqual(remove_from_list(lst, 3), [1, 2, 4]) | ||
self.assertEqual(remove_from_list(lst, 5), [1, 2, 3, 4]) # Item not in list | ||
|
||
def test_find_in_list(self): | ||
lst = [1, 2, 3, 4] | ||
self.assertTrue(find_in_list(lst, 3)) | ||
self.assertFalse(find_in_list(lst, 5)) | ||
|
||
def test_sort_list(self): | ||
lst = [3, 1, 4, 2] | ||
self.assertEqual(sort_list(lst), [1, 2, 3, 4]) | ||
self.assertEqual(sort_list([]), []) | ||
|
||
def test_merge_lists(self): | ||
lst1 = [1, 2] | ||
lst2 = [3, 4] | ||
self.assertEqual(merge_lists(lst1, lst2), [1, 2, 3, 4]) | ||
|
||
def test_unique_elements(self): | ||
lst = [1, 2, 2, 3, 4, 4] | ||
self.assertEqual(unique_elements(lst), [1, 2, 3, 4]) | ||
|
||
def test_list_average(self): | ||
lst = [1, 2, 3, 4] | ||
self.assertEqual(list_average(lst), 2.5) | ||
self.assertEqual(list_average([]), 0) # Edge case for empty list | ||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
|
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,4 @@ | ||
# __init__.py | ||
|
||
# This file allows the snippets folder to be treated as a package. | ||
# It can be empty or used to expose certain functions for easy imports. |
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,105 @@ | ||
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 | ||
======================== | ||
|
||
Introduction | ||
------------ | ||
|
||
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 | ||
------------- | ||
|
||
### Remove Duplicates | ||
|
||
This function removes duplicate elements from the list while maintaining the original order. | ||
|
||
remove_duplicates(lst) | ||
|
||
* **Args**: | ||
* `lst` (list): A list from which to remove duplicates. | ||
* **Returns**: | ||
* `list`: A list without duplicates. | ||
|
||
**Example**: | ||
|
||
>>> remove_duplicates([1, 2, 2, 3, 4, 4]) | ||
[1, 2, 3, 4] | ||
|
||
* * * | ||
|
||
### Flatten Nested List | ||
|
||
This function flattens a nested list into a single list. | ||
|
||
flatten_nested_list(nested_list) | ||
|
||
* **Args**: | ||
* `nested_list` (list): A nested list to flatten. | ||
* **Returns**: | ||
* `list`: A flattened list. | ||
|
||
**Example**: | ||
|
||
>>> flatten_nested_list([[1, 2], [3, 4], [5]]) | ||
[1, 2, 3, 4, 5] | ||
|
||
* * * | ||
|
||
### List Intersection | ||
|
||
This function finds the intersection of two lists. | ||
|
||
list_intersection(lst1, lst2) | ||
|
||
* **Args**: | ||
* `lst1` (list): The first list. | ||
* `lst2` (list): The second list. | ||
* **Returns**: | ||
* `list`: The intersection of the two lists. | ||
|
||
**Example**: | ||
|
||
>>> list_intersection([1, 2, 3], [2, 3, 4]) | ||
[2, 3] | ||
|
||
* * * | ||
|
||
### 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! |
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,62 @@ | ||
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) | ||
|
||
# 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)) | ||
|