From 1f40720763c6fc12c024a2bd14135652932be0d3 Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Mon, 7 Oct 2024 14:48:54 -0400 Subject: [PATCH 1/2] Added list manipulation methods --- pysnippets/Lists/__init__.py | 4 + pysnippets/Lists/list_manipulation.md | 105 ++++++++++++++++++++++++++ pysnippets/Lists/list_manipulation.py | 62 +++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 pysnippets/Lists/__init__.py create mode 100644 pysnippets/Lists/list_manipulation.md create mode 100644 pysnippets/Lists/list_manipulation.py diff --git a/pysnippets/Lists/__init__.py b/pysnippets/Lists/__init__.py new file mode 100644 index 00000000..c1125c68 --- /dev/null +++ b/pysnippets/Lists/__init__.py @@ -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. diff --git a/pysnippets/Lists/list_manipulation.md b/pysnippets/Lists/list_manipulation.md new file mode 100644 index 00000000..529e93da --- /dev/null +++ b/pysnippets/Lists/list_manipulation.md @@ -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! diff --git a/pysnippets/Lists/list_manipulation.py b/pysnippets/Lists/list_manipulation.py new file mode 100644 index 00000000..7d7edc68 --- /dev/null +++ b/pysnippets/Lists/list_manipulation.py @@ -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)) + From 50c66b28a2061cc5bfb9ddc634f96a5376598a53 Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Mon, 7 Oct 2024 15:32:43 -0400 Subject: [PATCH 2/2] added test cases --- Tests/lists/test_list_manipulation.py | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Tests/lists/test_list_manipulation.py diff --git a/Tests/lists/test_list_manipulation.py b/Tests/lists/test_list_manipulation.py new file mode 100644 index 00000000..790ea726 --- /dev/null +++ b/Tests/lists/test_list_manipulation.py @@ -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() +