From 5b3a3e9d371624a18e8fb3d045ccc68bf0e25f9d Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:11:24 +0530 Subject: [PATCH 01/27] Create MajorityVoteAlgorithm.py --- other/MajorityVoteAlgorithm.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 other/MajorityVoteAlgorithm.py diff --git a/other/MajorityVoteAlgorithm.py b/other/MajorityVoteAlgorithm.py new file mode 100644 index 000000000000..4fa88c8e8c9f --- /dev/null +++ b/other/MajorityVoteAlgorithm.py @@ -0,0 +1,24 @@ +''' +This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. +We have to solve in O(n) time and O(1) Space. + +''' +import collections + +def majorityElement(nums, k): + ctr = collections.Counter() + for n in nums: + ctr[n] += 1 + if len(ctr) == k: + ctr -= collections.Counter(set(ctr)) + ctr = collections.Counter(n for n in nums if n in ctr) + return [n for n in ctr if ctr[n] > len(nums)/k] + + +nums = [1,2,2,3,1,3,2] +k = 3 +print(majorityElement(nums,k)) + +''' +Output: [2] +''' From 6495340b4696d84fe9ebeb2a41470bba5d72e96c Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:14:54 +0530 Subject: [PATCH 02/27] Update and rename MajorityVoteAlgorithm.py to majorityvotealgorithm.py --- other/{MajorityVoteAlgorithm.py => majorityvotealgorithm.py} | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) rename other/{MajorityVoteAlgorithm.py => majorityvotealgorithm.py} (80%) diff --git a/other/MajorityVoteAlgorithm.py b/other/majorityvotealgorithm.py similarity index 80% rename from other/MajorityVoteAlgorithm.py rename to other/majorityvotealgorithm.py index 4fa88c8e8c9f..c133d1f01348 100644 --- a/other/MajorityVoteAlgorithm.py +++ b/other/majorityvotealgorithm.py @@ -1,11 +1,12 @@ ''' This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. +URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm ''' import collections -def majorityElement(nums, k): +def majorityElement(self, nums: List[int]) -> List[int]: ctr = collections.Counter() for n in nums: ctr[n] += 1 From b881325aef5e6f365c05e9c6c45e8541628c4695 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 19:49:07 +0000 Subject: [PATCH 03/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majorityvotealgorithm.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/other/majorityvotealgorithm.py b/other/majorityvotealgorithm.py index c133d1f01348..319edfaa6fa6 100644 --- a/other/majorityvotealgorithm.py +++ b/other/majorityvotealgorithm.py @@ -1,25 +1,26 @@ -''' +""" This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. -We have to solve in O(n) time and O(1) Space. +We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm - -''' + +""" import collections + def majorityElement(self, nums: List[int]) -> List[int]: - ctr = collections.Counter() - for n in nums: - ctr[n] += 1 - if len(ctr) == k: - ctr -= collections.Counter(set(ctr)) - ctr = collections.Counter(n for n in nums if n in ctr) - return [n for n in ctr if ctr[n] > len(nums)/k] + ctr = collections.Counter() + for n in nums: + ctr[n] += 1 + if len(ctr) == k: + ctr -= collections.Counter(set(ctr)) + ctr = collections.Counter(n for n in nums if n in ctr) + return [n for n in ctr if ctr[n] > len(nums) / k] -nums = [1,2,2,3,1,3,2] +nums = [1, 2, 2, 3, 1, 3, 2] k = 3 -print(majorityElement(nums,k)) +print(majorityElement(nums, k)) -''' +""" Output: [2] -''' +""" From e3580d4c1b096b7b50125c9d8d13e910911d7ccd Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:28:26 +0530 Subject: [PATCH 04/27] Update and rename majorityvotealgorithm.py to majority_vote_algorithm.py --- ...algorithm.py => majority_vote_algorithm.py} | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) rename other/{majorityvotealgorithm.py => majority_vote_algorithm.py} (77%) diff --git a/other/majorityvotealgorithm.py b/other/majority_vote_algorithm.py similarity index 77% rename from other/majorityvotealgorithm.py rename to other/majority_vote_algorithm.py index 319edfaa6fa6..b5843816d37f 100644 --- a/other/majorityvotealgorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,13 +1,18 @@ +import collections """ This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm """ -import collections -def majorityElement(self, nums: List[int]) -> List[int]: + +def majority_element(nums,k): + """ + >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) + [2] + """ ctr = collections.Counter() for n in nums: ctr[n] += 1 @@ -17,10 +22,7 @@ def majorityElement(self, nums: List[int]) -> List[int]: return [n for n in ctr if ctr[n] > len(nums) / k] -nums = [1, 2, 2, 3, 1, 3, 2] -k = 3 -print(majorityElement(nums, k)) -""" -Output: [2] -""" +if __name__ == "__main__": + import doctest + doctest.testmod() From 2398221bcd89deabeaec313d64248ec05514ba2c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 19:59:08 +0000 Subject: [PATCH 05/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index b5843816d37f..6bb8586b9c07 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,4 +1,5 @@ import collections + """ This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. @@ -7,8 +8,7 @@ """ - -def majority_element(nums,k): +def majority_element(nums, k): """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] @@ -22,7 +22,7 @@ def majority_element(nums,k): return [n for n in ctr if ctr[n] > len(nums) / k] - if __name__ == "__main__": import doctest + doctest.testmod() From ba13c2a64d512d43a32f675ba11733f2357a9d40 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:29:58 +0530 Subject: [PATCH 06/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 6bb8586b9c07..4429bee3b95c 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -8,7 +8,7 @@ """ -def majority_element(nums, k): +def majority_element(nums : List[int], k : int) -> List[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] From 83413d19060691820ed58c0e761d98c4fd886106 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 20:00:32 +0000 Subject: [PATCH 07/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 4429bee3b95c..2f1380c5448d 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -8,7 +8,7 @@ """ -def majority_element(nums : List[int], k : int) -> List[int]: +def majority_element(nums: List[int], k: int) -> List[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] From 2bcd208278779bdee7507a1e76f066fdd62fade1 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:31:55 +0530 Subject: [PATCH 08/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 2f1380c5448d..5d13d1a4d6d2 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -8,7 +8,7 @@ """ -def majority_element(nums: List[int], k: int) -> List[int]: +def majority_element(nums: List[int], partition: int) -> List[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] @@ -16,10 +16,10 @@ def majority_element(nums: List[int], k: int) -> List[int]: ctr = collections.Counter() for n in nums: ctr[n] += 1 - if len(ctr) == k: + if len(ctr) == partition: ctr -= collections.Counter(set(ctr)) ctr = collections.Counter(n for n in nums if n in ctr) - return [n for n in ctr if ctr[n] > len(nums) / k] + return [n for n in ctr if ctr[n] > len(nums) / partition] if __name__ == "__main__": From 88efe5b32329f4fe62560a9b49eefca1b64b388b Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:28:15 +0530 Subject: [PATCH 09/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 5d13d1a4d6d2..4ad22a86ccbe 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -8,7 +8,7 @@ """ -def majority_element(nums: List[int], partition: int) -> List[int]: +def majority_element(nums: list[int], partition: int) -> list[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] From b0608a0fa65e050d68a359af35b517f62909d741 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:12:20 +0530 Subject: [PATCH 10/27] Update other/majority_vote_algorithm.py Co-authored-by: Christian Clauss --- other/majority_vote_algorithm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 4ad22a86ccbe..175e361d3479 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,7 +1,8 @@ import collections """ -This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. +This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like: +Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm From 6ff610a1ac2668f7ecd51ef49badda45f1025e9e Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:33:25 +0530 Subject: [PATCH 11/27] renaming variables majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 175e361d3479..93947ffbdcb5 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,29 +1,28 @@ import collections - """ This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like: Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm - """ -def majority_element(nums: list[int], partition: int) -> list[int]: +def majority_element(total_votes: list[int], min_votes_required: int) -> list[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - ctr = collections.Counter() - for n in nums: - ctr[n] += 1 - if len(ctr) == partition: - ctr -= collections.Counter(set(ctr)) - ctr = collections.Counter(n for n in nums if n in ctr) - return [n for n in ctr if ctr[n] > len(nums) / partition] + majority_candidate_counter : Dict[int, int] = collections.Counter() + for vote in total_votes: + majority_candidate_counter[vote] += 1 + if len(majority_candidate_counter) == min_votes_required: + majority_candidate_counter-=collections.Counter(set(majority_candidate_counter)) + majority_candidate_counter = collections.Counter(vote for vote in total_votes + if vote in majority_candidate_counter) + return [vote for vote in majority_candidate_counter + if majority_candidate_counter[vote] > len(total_votes) / min_votes_required] if __name__ == "__main__": import doctest - doctest.testmod() From 288608e07d7d034906107b4ffc1a97e09cae3003 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 11:04:29 +0000 Subject: [PATCH 12/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 93947ffbdcb5..d8ca053a942c 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,4 +1,5 @@ import collections + """ This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like: Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. @@ -12,17 +13,24 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter : Dict[int, int] = collections.Counter() + majority_candidate_counter: Dict[int, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: - majority_candidate_counter-=collections.Counter(set(majority_candidate_counter)) - majority_candidate_counter = collections.Counter(vote for vote in total_votes - if vote in majority_candidate_counter) - return [vote for vote in majority_candidate_counter - if majority_candidate_counter[vote] > len(total_votes) / min_votes_required] + majority_candidate_counter -= collections.Counter( + set(majority_candidate_counter) + ) + majority_candidate_counter = collections.Counter( + vote for vote in total_votes if vote in majority_candidate_counter + ) + return [ + vote + for vote in majority_candidate_counter + if majority_candidate_counter[vote] > len(total_votes) / min_votes_required + ] if __name__ == "__main__": import doctest + doctest.testmod() From cf8a2e393f0a89e3fc1752d910f850055cebad04 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:40:00 +0530 Subject: [PATCH 13/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index d8ca053a942c..34226fab0a95 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -13,24 +13,15 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter: Dict[int, int] = collections.Counter() + majority_candidate_counter : dict[int, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: - majority_candidate_counter -= collections.Counter( - set(majority_candidate_counter) - ) - majority_candidate_counter = collections.Counter( - vote for vote in total_votes if vote in majority_candidate_counter - ) - return [ - vote - for vote in majority_candidate_counter - if majority_candidate_counter[vote] > len(total_votes) / min_votes_required - ] + majority_candidate_counter -= collections.Counter(set(majority_candidate_counter)) + majority_candidate_counter = collections.Counter(vote for vote in total_votes if vote in majority_candidate_counter) + return [vote for vote in majority_candidate_counter if majority_candidate_counter[vote] > len(total_votes) / min_votes_required] if __name__ == "__main__": import doctest - doctest.testmod() From 7cf0e5dab83a3648ee910c04ef098e9a17d794cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 11:13:13 +0000 Subject: [PATCH 14/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 34226fab0a95..dd2647564d48 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -13,15 +13,24 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter : dict[int, int] = collections.Counter() + majority_candidate_counter: dict[int, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: - majority_candidate_counter -= collections.Counter(set(majority_candidate_counter)) - majority_candidate_counter = collections.Counter(vote for vote in total_votes if vote in majority_candidate_counter) - return [vote for vote in majority_candidate_counter if majority_candidate_counter[vote] > len(total_votes) / min_votes_required] + majority_candidate_counter -= collections.Counter( + set(majority_candidate_counter) + ) + majority_candidate_counter = collections.Counter( + vote for vote in total_votes if vote in majority_candidate_counter + ) + return [ + vote + for vote in majority_candidate_counter + if majority_candidate_counter[vote] > len(total_votes) / min_votes_required + ] if __name__ == "__main__": import doctest + doctest.testmod() From fbd6082f6f32cd33c32c125572d3b544b65457c3 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:48:17 +0530 Subject: [PATCH 15/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index dd2647564d48..ac1b9bf027ee 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -13,7 +13,7 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter: dict[int, int] = collections.Counter() + majority_candidate_counter: dict[str, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: From 21f5635880ac899a853cc8ff40f254e87fdaaf4e Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:57:20 +0530 Subject: [PATCH 16/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index ac1b9bf027ee..d65f42f75bc8 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -13,7 +13,7 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter: dict[str, int] = collections.Counter() + majority_candidate_counter: Counter[int, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: From 57e64cef05bac2e505448499068e32048c62682e Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:02:46 +0530 Subject: [PATCH 17/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index d65f42f75bc8..153bf6ecbde9 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -13,7 +13,7 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter: Counter[int, int] = collections.Counter() + majority_candidate_counter: counter[int, int] = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == min_votes_required: From 23ea6febdcf4d0a6d8f830cd5ce2230516b6ffa4 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:10:03 +0530 Subject: [PATCH 18/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 153bf6ecbde9..6d5491696930 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -7,16 +7,15 @@ URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm """ - -def majority_element(total_votes: list[int], min_votes_required: int) -> list[int]: +def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter: counter[int, int] = collections.Counter() + majority_candidate_counter = collections.Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 - if len(majority_candidate_counter) == min_votes_required: + if len(majority_candidate_counter) == max_candidates: majority_candidate_counter -= collections.Counter( set(majority_candidate_counter) ) @@ -26,7 +25,7 @@ def majority_element(total_votes: list[int], min_votes_required: int) -> list[in return [ vote for vote in majority_candidate_counter - if majority_candidate_counter[vote] > len(total_votes) / min_votes_required + if majority_candidate_counter[vote] > len(total_votes) / max_candidates ] From 5131a199fc4de00d20dabef46827d145298f3964 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 11:40:43 +0000 Subject: [PATCH 19/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 6d5491696930..95fd4db795a9 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -7,6 +7,7 @@ URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm """ + def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) From 5e398b2b355761ca7ecb3d344999ad8d95bef492 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:37:10 +0530 Subject: [PATCH 20/27] Update other/majority_vote_algorithm.py Co-authored-by: Christian Clauss --- other/majority_vote_algorithm.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 95fd4db795a9..7899633b1d57 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,11 +1,10 @@ -import collections - """ This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like: Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm """ +from collections import Counter def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: @@ -13,14 +12,14 @@ def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) [2] """ - majority_candidate_counter = collections.Counter() + majority_candidate_counter: Counter[int] = Counter() for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == max_candidates: - majority_candidate_counter -= collections.Counter( + majority_candidate_counter -= Counter( set(majority_candidate_counter) ) - majority_candidate_counter = collections.Counter( + majority_candidate_counter = Counter( vote for vote in total_votes if vote in majority_candidate_counter ) return [ From 9d2e3dc4b14ef6af2a1c7334fd5f7ea14a2e0d27 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:08:43 +0000 Subject: [PATCH 21/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index 7899633b1d57..c77fd0b4cd46 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -16,9 +16,7 @@ def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: for vote in total_votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == max_candidates: - majority_candidate_counter -= Counter( - set(majority_candidate_counter) - ) + majority_candidate_counter -= Counter(set(majority_candidate_counter)) majority_candidate_counter = Counter( vote for vote in total_votes if vote in majority_candidate_counter ) From 00c65aa61f39cf26817e432638ec4af3e35bfe39 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:02:12 +0530 Subject: [PATCH 22/27] Update other/majority_vote_algorithm.py Co-authored-by: Christian Clauss --- other/majority_vote_algorithm.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index c77fd0b4cd46..c704fe56897b 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -9,8 +9,12 @@ def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: """ - >>> majority_element([1, 2, 2, 3, 1, 3, 2],3) + >>> majority_element([1, 2, 2, 3, 1, 3, 2], 3) [2] + >>> majority_element([1, 2, 2, 3, 1, 3, 2], 2) + ? + >>> majority_element([1, 2, 2, 3, 1, 3, 2], 4) + ? """ majority_candidate_counter: Counter[int] = Counter() for vote in total_votes: From a263f7a00ea05442fa4f409aeddeea9993c39a40 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:10:57 +0530 Subject: [PATCH 23/27] adding more testcases majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index c704fe56897b..e2f5ff7ea854 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -7,27 +7,27 @@ from collections import Counter -def majority_element(total_votes: list[int], max_candidates: int) -> list[int]: +def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: """ >>> majority_element([1, 2, 2, 3, 1, 3, 2], 3) [2] >>> majority_element([1, 2, 2, 3, 1, 3, 2], 2) - ? + [] >>> majority_element([1, 2, 2, 3, 1, 3, 2], 4) - ? + [1, 2, 3] """ - majority_candidate_counter: Counter[int] = Counter() - for vote in total_votes: + majority_candidate_counter = Counter() + for vote in votes: majority_candidate_counter[vote] += 1 - if len(majority_candidate_counter) == max_candidates: + if len(majority_candidate_counter) == votes_needed_to_win : majority_candidate_counter -= Counter(set(majority_candidate_counter)) majority_candidate_counter = Counter( - vote for vote in total_votes if vote in majority_candidate_counter + vote for vote in votes if vote in majority_candidate_counter ) return [ vote for vote in majority_candidate_counter - if majority_candidate_counter[vote] > len(total_votes) / max_candidates + if majority_candidate_counter[vote] > len(votes) / votes_needed_to_win ] From 63d60e958ca54065da050d4264d4411074a88487 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:42:01 +0000 Subject: [PATCH 24/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index e2f5ff7ea854..dccc1cbf5f38 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -19,7 +19,7 @@ def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: majority_candidate_counter = Counter() for vote in votes: majority_candidate_counter[vote] += 1 - if len(majority_candidate_counter) == votes_needed_to_win : + if len(majority_candidate_counter) == votes_needed_to_win: majority_candidate_counter -= Counter(set(majority_candidate_counter)) majority_candidate_counter = Counter( vote for vote in votes if vote in majority_candidate_counter @@ -27,7 +27,7 @@ def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: return [ vote for vote in majority_candidate_counter - if majority_candidate_counter[vote] > len(votes) / votes_needed_to_win + if majority_candidate_counter[vote] > len(votes) / votes_needed_to_win ] From 6756885cb0058c4193e3a1d1361fff21da801507 Mon Sep 17 00:00:00 2001 From: Sarvjeet Singh <63469455+aazad20@users.noreply.github.com> Date: Fri, 6 Oct 2023 18:35:26 +0530 Subject: [PATCH 25/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index dccc1cbf5f38..ca6c630899f7 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -16,7 +16,7 @@ def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: >>> majority_element([1, 2, 2, 3, 1, 3, 2], 4) [1, 2, 3] """ - majority_candidate_counter = Counter() + majority_candidate_counter : Counter[int] = Counter() for vote in votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == votes_needed_to_win: From a215f1b6123e0b8e9613594463d93d5f5373b64a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:09:05 +0000 Subject: [PATCH 26/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/majority_vote_algorithm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index ca6c630899f7..dc55084cff10 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -16,7 +16,7 @@ def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: >>> majority_element([1, 2, 2, 3, 1, 3, 2], 4) [1, 2, 3] """ - majority_candidate_counter : Counter[int] = Counter() + majority_candidate_counter: Counter[int] = Counter() for vote in votes: majority_candidate_counter[vote] += 1 if len(majority_candidate_counter) == votes_needed_to_win: From c0a3f88fc344dbe589137e3a7c3997e93884b25a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 6 Oct 2023 15:42:41 +0200 Subject: [PATCH 27/27] Update majority_vote_algorithm.py --- other/majority_vote_algorithm.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/other/majority_vote_algorithm.py b/other/majority_vote_algorithm.py index dc55084cff10..ab8b386dd2e5 100644 --- a/other/majority_vote_algorithm.py +++ b/other/majority_vote_algorithm.py @@ -1,5 +1,5 @@ """ -This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like: +This is Booyer-Moore Majority Vote Algorithm. The problem statement goes like this: Given an integer array of size n, find all elements that appear more than ⌊ n/k ⌋ times. We have to solve in O(n) time and O(1) Space. URL : https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm @@ -7,13 +7,13 @@ from collections import Counter -def majority_element(votes: list[int], votes_needed_to_win: int) -> list[int]: +def majority_vote(votes: list[int], votes_needed_to_win: int) -> list[int]: """ - >>> majority_element([1, 2, 2, 3, 1, 3, 2], 3) + >>> majority_vote([1, 2, 2, 3, 1, 3, 2], 3) [2] - >>> majority_element([1, 2, 2, 3, 1, 3, 2], 2) + >>> majority_vote([1, 2, 2, 3, 1, 3, 2], 2) [] - >>> majority_element([1, 2, 2, 3, 1, 3, 2], 4) + >>> majority_vote([1, 2, 2, 3, 1, 3, 2], 4) [1, 2, 3] """ majority_candidate_counter: Counter[int] = Counter()