From fbdb60fe921a446a726f7c026417a60cffa65c5a Mon Sep 17 00:00:00 2001 From: ca20110820 Date: Sun, 14 Apr 2024 11:31:21 +0800 Subject: [PATCH 1/3] fix: remove_duplicates function when dealing with binary values 1 and True are treated the same 0 and False are treated the same --- algorithms/arrays/remove_duplicates.py | 13 ++++++++----- tests/test_array.py | 21 ++++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/algorithms/arrays/remove_duplicates.py b/algorithms/arrays/remove_duplicates.py index 1c0bc0a06..b4cdfec38 100644 --- a/algorithms/arrays/remove_duplicates.py +++ b/algorithms/arrays/remove_duplicates.py @@ -4,15 +4,18 @@ For example: -Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True] -Output: [1, 2, 3, 4, 'hey', 'hello'] +Input: [1, 1 ,1 ,2 ,2 ,3 ,4 ,4 ,"hey", "hey", "hello", True, True, False] +Output: [1, 2, 3, 4, 'hey', 'hello', True, False] """ + def remove_duplicates(array): - new_array = [] + new_array = [] # Preserve order of first distinct item + seen = set() # Track the unique items for item in array: - if item not in new_array: + if all([item is not elem for elem in seen]): new_array.append(item) + seen.add(item) - return new_array \ No newline at end of file + return new_array diff --git a/tests/test_array.py b/tests/test_array.py index f1ad11693..ad0316138 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -9,7 +9,7 @@ missing_ranges, move_zeros, plus_one_v1, plus_one_v2, plus_one_v3, - remove_duplicates + remove_duplicates, rotate_v1, rotate_v2, rotate_v3, summarize_ranges, three_sum, @@ -299,14 +299,21 @@ def test_plus_one_v3(self): self.assertListEqual(plus_one_v3([9, 9, 9, 9]), [1, 0, 0, 0, 0]) -class TestRemoveDuplicate(unittest.TestCase): +class TestRemoveDuplicate(unittest.TestCase): def test_remove_duplicates(self): - self.assertListEqual(remove_duplicates([1,1,1,2,2,2,3,3,4,4,5,6,7,7,7,8,8,9,10,10])) - self.assertListEqual(remove_duplicates(["hey", "hello", "hello", "car", "house", "house"])) - self.assertListEqual(remove_duplicates([True, True, False, True, False, None, None])) - self.assertListEqual(remove_duplicates([1,1,"hello", "hello", True, False, False])) - self.assertListEqual(remove_duplicates([1, "hello", True, False])) + self.assertListEqual(remove_duplicates([1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 7, 8, 8, 9, 10, 10]), + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + self.assertListEqual(remove_duplicates(["hey", "hello", "hello", "car", "house", "house"]), + ["hey", "hello", "car", "house"]) + self.assertListEqual(remove_duplicates([True, True, False, True, False, None, None]), + [True, False, None]) + self.assertListEqual(remove_duplicates([1, 1, "hello", "hello", True, False, False]), + [1, "hello", True, False]) + self.assertListEqual(remove_duplicates([1, "hello", True, False]), + [1, "hello", True, False]) + self.assertListEqual(remove_duplicates([False, True, False, True]), + [False, True]) class TestRotateArray(unittest.TestCase): From 7462c54e1118a658f900df7a4f27ea7129471a69 Mon Sep 17 00:00:00 2001 From: ca20110820 Date: Sun, 14 Apr 2024 11:40:27 +0800 Subject: [PATCH 2/3] docs: add docstring for remove_duplicates function --- algorithms/arrays/remove_duplicates.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/algorithms/arrays/remove_duplicates.py b/algorithms/arrays/remove_duplicates.py index b4cdfec38..fc1fc9f6c 100644 --- a/algorithms/arrays/remove_duplicates.py +++ b/algorithms/arrays/remove_duplicates.py @@ -10,6 +10,18 @@ def remove_duplicates(array): + """Removes duplicates from the input array while preserving the order of the first occurrence of each distinct item. + + Args: + array (list): The input array containing elements with potential duplicates. + + Returns: + list: A new array with duplicates removed, maintaining the order of the first occurrence of each item. + + Examples: + >>> remove_duplicates([1, 1, 1, 2, 2, 3, 4, 4, "hey", "hey", "hello", True, True, False]) + [1, 2, 3, 4, 'hey', 'hello', True, False] + """ new_array = [] # Preserve order of first distinct item seen = set() # Track the unique items From 123ef62cc9c2f5f997a89b841e5b221e9e941e0b Mon Sep 17 00:00:00 2001 From: ca20110820 Date: Sun, 14 Apr 2024 12:07:45 +0800 Subject: [PATCH 3/3] fix: fixed test_summarize_ranges method in test_array --- tests/test_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_array.py b/tests/test_array.py index ad0316138..c9026644b 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -354,11 +354,11 @@ class TestSummaryRanges(unittest.TestCase): def test_summarize_ranges(self): self.assertListEqual(summarize_ranges([0, 1, 2, 4, 5, 7]), - [(0, 2), (4, 5), (7, 7)]) + ['0-2', '4-5', '7']) self.assertListEqual(summarize_ranges([-5, -4, -3, 1, 2, 4, 5, 6]), - [(-5, -3), (1, 2), (4, 6)]) + ['-5--3', '1-2', '4-6']) self.assertListEqual(summarize_ranges([-2, -1, 0, 1, 2]), - [(-2, 2)]) + ['-2-2']) class TestThreeSum(unittest.TestCase):