From 0f5d1e4d013b09f0f78f5c72318f907fc14f70b1 Mon Sep 17 00:00:00 2001 From: Vinesh nayak Date: Sat, 28 Mar 2026 10:10:08 +0530 Subject: [PATCH 1/3] Create add function to sum two integers Implement add function with type hints and input validation. --- math/add.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 math/add.py diff --git a/math/add.py b/math/add.py new file mode 100644 index 000000000000..604075ab8ad1 --- /dev/null +++ b/math/add.py @@ -0,0 +1,12 @@ +def add(a: int, b: int) -> int: + """ + Return sum of two integers + + >>> add(2, 3) + 5 + >>> add(-1, 1) + 0 + """ + if not isinstance(a, int) or not isinstance(b, int): + raise ValueError("Inputs must be integers") + return a + b From e3df72581d4c9cad5d4665a4caa4613a729d3e26 Mon Sep 17 00:00:00 2001 From: Vinesh nayak Date: Sat, 28 Mar 2026 10:16:49 +0530 Subject: [PATCH 2/3] Add majority_element function using Boyer-Moore algorithm Implement Boyer-Moore Voting Algorithm to find majority element. --- searches/boyer_moore_majority_vote.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 searches/boyer_moore_majority_vote.py diff --git a/searches/boyer_moore_majority_vote.py b/searches/boyer_moore_majority_vote.py new file mode 100644 index 000000000000..a5a4ae4fa825 --- /dev/null +++ b/searches/boyer_moore_majority_vote.py @@ -0,0 +1,23 @@ +def majority_element(nums: list[int]) -> int: + """ + Find majority element using Boyer-Moore Voting Algorithm + https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm + + >>> majority_element([3, 3, 4]) + 3 + >>> majority_element([2, 2, 1, 1, 2, 2, 2]) + 2 + """ + + if not nums: + raise ValueError("List cannot be empty") + + candidate = None + count = 0 + + for num in nums: + if count == 0: + candidate = num + count += (1 if num == candidate else -1) + + return candidate From a018dc4a25da826206a82fff7b4c5029bb85530c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Mar 2026 04:54:14 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/boyer_moore_majority_vote.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/boyer_moore_majority_vote.py b/searches/boyer_moore_majority_vote.py index a5a4ae4fa825..b11592fa1a1e 100644 --- a/searches/boyer_moore_majority_vote.py +++ b/searches/boyer_moore_majority_vote.py @@ -18,6 +18,6 @@ def majority_element(nums: list[int]) -> int: for num in nums: if count == 0: candidate = num - count += (1 if num == candidate else -1) + count += 1 if num == candidate else -1 return candidate