From 636295e643e03e876b0a605af282ba59a461a9e7 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 09:48:17 +0530 Subject: [PATCH 01/11] add Three sum --- maths/three_sum.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 maths/three_sum.py diff --git a/maths/three_sum.py b/maths/three_sum.py new file mode 100644 index 000000000000..633b8f4fd91f --- /dev/null +++ b/maths/three_sum.py @@ -0,0 +1,45 @@ +def threeSum(nums: list[int]) -> list[list[int]]: + """ + Find all unique triplets in a sorted array of integers that sum up to zero. + + Args: + nums (list[int]): A sorted list of integers. + + Returns: + list[list[int]]: A list of lists containing unique triplets that sum up to zero. + + Example: + >>> threeSum([-1, 0, 1, 2, -1, -4]) + [[-1, -1, 2], [-1, 0, 1]] + >>> threeSum([1, 2, 3, 4]) + [] + + """ + nums.sort() + ans = [] + for i in range(len(nums) - 2): + if i == 0 or (nums[i] != nums[i - 1]): + low, high, c = i + 1, len(nums) - 1, 0 - nums[i] + while low < high: + if nums[low] + nums[high] == c: + ans.append([nums[i], nums[low], nums[high]]) + + while low < high and nums[low] == nums[low + 1]: + low += 1 + while low < high and nums[high] == nums[high - 1]: + high -= 1 + + low += 1 + high -= 1 + elif nums[low] + nums[high] < c: + low += 1 + else: + high -= 1 + return ans + +# Run the doctests +if __name__ == "__main__": + import doctest + + doctest.testmod() + \ No newline at end of file From be74c5ffe1866e87776993c05844a00f97f86bf9 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 09:54:19 +0530 Subject: [PATCH 02/11] add Three sum --- maths/three_sum.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index 633b8f4fd91f..4ea62d7953ed 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -42,4 +42,3 @@ def threeSum(nums: list[int]) -> list[list[int]]: import doctest doctest.testmod() - \ No newline at end of file From 307d4cf65551ab6ab8ca393aef5170c36701aee8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 04:25:14 +0000 Subject: [PATCH 03/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/three_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/three_sum.py b/maths/three_sum.py index 4ea62d7953ed..8fc41b50a2a1 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -37,6 +37,7 @@ def threeSum(nums: list[int]) -> list[list[int]]: high -= 1 return ans + # Run the doctests if __name__ == "__main__": import doctest From 3d688e95253c26ce78d208d1fca46341ba2ffd11 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 09:57:17 +0530 Subject: [PATCH 04/11] update --- maths/three_sum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index 4ea62d7953ed..823f1ce1cc18 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,4 +1,4 @@ -def threeSum(nums: list[int]) -> list[list[int]]: +def three_sum(nums: list[int]) -> list[list[int]]: """ Find all unique triplets in a sorted array of integers that sum up to zero. @@ -9,9 +9,9 @@ def threeSum(nums: list[int]) -> list[list[int]]: list[list[int]]: A list of lists containing unique triplets that sum up to zero. Example: - >>> threeSum([-1, 0, 1, 2, -1, -4]) + >>> three_sum([-1, 0, 1, 2, -1, -4]) [[-1, -1, 2], [-1, 0, 1]] - >>> threeSum([1, 2, 3, 4]) + >>> three_sum([1, 2, 3, 4]) [] """ From d5ebfd7b0b504aaeb8b82ceb141f4b674317344f Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 19:41:04 +0530 Subject: [PATCH 05/11] update --- maths/three_sum.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index d7a85bad871a..20aa7927cb3d 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,12 +1,12 @@ -def three_sum(nums: list[int]) -> list[list[int]]: +def three_sum(nums: list[int]) -> tuple[tuple[int]]: """ Find all unique triplets in a sorted array of integers that sum up to zero. Args: - nums (list[int]): A sorted list of integers. + nums: A sorted list of integers. Returns: - list[list[int]]: A list of lists containing unique triplets that sum up to zero. + A list of lists containing unique triplets that sum up to zero. Example: >>> three_sum([-1, 0, 1, 2, -1, -4]) From 80842b81e541c6e6f9b4b60b46552710600f35f2 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 19:56:45 +0530 Subject: [PATCH 06/11] update --- maths/three_sum.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index 20aa7927cb3d..382f0d1407bc 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,4 +1,18 @@ -def three_sum(nums: list[int]) -> tuple[tuple[int]]: +""" +The "Three Sum" problem is a commonly encountered problem in computer science and data analysis. + +1. **Data Analysis**: In data analysis and statistics, the Three Sum problem can be used to identify sets of data points that, when combined, result in a specific outcome or condition. For example, it can be applied to financial data to find combinations of transactions that balance an account. + +2. **Optimization**: In optimization problems, such as resource allocation or scheduling, the Three Sum problem can help identify combinations of resources or tasks that result in a desired outcome. This is valuable in industries like logistics and project management. + +3. **Algorithmic Research**: Researchers and algorithm designers often use the Three Sum problem as a benchmark for evaluating the efficiency and performance of algorithms. It serves as a challenging problem to test new algorithmic techniques. + +4. **Cryptography**: Some cryptographic protocols involve operations on large numbers or sets of numbers. The Three Sum problem can be used as part of cryptographic algorithms to verify or validate certain conditions. + +In summary, the Three Sum problem has practical applications in various fields, including data analysis, optimization, algorithmic research, pattern recognition, and cryptography. Its versatility makes it a valuable problem for both theoretical analysis and real-world problem-solving. +""" + +def three_sum(nums: list[int]) -> list[list[int]]: """ Find all unique triplets in a sorted array of integers that sum up to zero. From 6fed286b7c79c2276246eacc835290f7a382bd9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:27:36 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/three_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/three_sum.py b/maths/three_sum.py index 382f0d1407bc..e0b6038d3f05 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -12,6 +12,7 @@ In summary, the Three Sum problem has practical applications in various fields, including data analysis, optimization, algorithmic research, pattern recognition, and cryptography. Its versatility makes it a valuable problem for both theoretical analysis and real-world problem-solving. """ + def three_sum(nums: list[int]) -> list[list[int]]: """ Find all unique triplets in a sorted array of integers that sum up to zero. From ac4f197426cf8377df1fc786d6a2da1c6da1a219 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 19:59:58 +0530 Subject: [PATCH 08/11] update --- maths/three_sum.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index 382f0d1407bc..bd9efcfe5018 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,15 +1,10 @@ """ -The "Three Sum" problem is a commonly encountered problem in computer science and data analysis. +The "Three Sum" problem is a commonly encountered problem in computer +science and data analysis.the Three Sum problem has practical applications +in various fields, including data analysis, optimization, algorithmic research, +pattern recognition, and cryptography. Its versatility makes it a valuable problem +for both theoretical analysis and real-world problem-solving. -1. **Data Analysis**: In data analysis and statistics, the Three Sum problem can be used to identify sets of data points that, when combined, result in a specific outcome or condition. For example, it can be applied to financial data to find combinations of transactions that balance an account. - -2. **Optimization**: In optimization problems, such as resource allocation or scheduling, the Three Sum problem can help identify combinations of resources or tasks that result in a desired outcome. This is valuable in industries like logistics and project management. - -3. **Algorithmic Research**: Researchers and algorithm designers often use the Three Sum problem as a benchmark for evaluating the efficiency and performance of algorithms. It serves as a challenging problem to test new algorithmic techniques. - -4. **Cryptography**: Some cryptographic protocols involve operations on large numbers or sets of numbers. The Three Sum problem can be used as part of cryptographic algorithms to verify or validate certain conditions. - -In summary, the Three Sum problem has practical applications in various fields, including data analysis, optimization, algorithmic research, pattern recognition, and cryptography. Its versatility makes it a valuable problem for both theoretical analysis and real-world problem-solving. """ def three_sum(nums: list[int]) -> list[list[int]]: From 8528b191d8b03c0a87511bab68f6d6e7477f28c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 14:30:47 +0000 Subject: [PATCH 09/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/three_sum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index ecc88544960f..d5f2ade842da 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,8 +1,8 @@ """ -The "Three Sum" problem is a commonly encountered problem in computer -science and data analysis.the Three Sum problem has practical applications -in various fields, including data analysis, optimization, algorithmic research, -pattern recognition, and cryptography. Its versatility makes it a valuable problem +The "Three Sum" problem is a commonly encountered problem in computer +science and data analysis.the Three Sum problem has practical applications +in various fields, including data analysis, optimization, algorithmic research, +pattern recognition, and cryptography. Its versatility makes it a valuable problem for both theoretical analysis and real-world problem-solving. """ From 730c7323c7c9ca217f5ef79e38397f6da4c7b7fe Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 1 Oct 2023 20:01:50 +0530 Subject: [PATCH 10/11] add documention --- maths/three_sum.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index ecc88544960f..d5f2ade842da 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,8 +1,8 @@ """ -The "Three Sum" problem is a commonly encountered problem in computer -science and data analysis.the Three Sum problem has practical applications -in various fields, including data analysis, optimization, algorithmic research, -pattern recognition, and cryptography. Its versatility makes it a valuable problem +The "Three Sum" problem is a commonly encountered problem in computer +science and data analysis.the Three Sum problem has practical applications +in various fields, including data analysis, optimization, algorithmic research, +pattern recognition, and cryptography. Its versatility makes it a valuable problem for both theoretical analysis and real-world problem-solving. """ From 4fe12be2ad34ace71d5c3469d07e3e2d0160de1f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 1 Oct 2023 16:45:54 +0200 Subject: [PATCH 11/11] Update three_sum.py --- maths/three_sum.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/maths/three_sum.py b/maths/three_sum.py index d5f2ade842da..09956f8415a0 100644 --- a/maths/three_sum.py +++ b/maths/three_sum.py @@ -1,10 +1,5 @@ """ -The "Three Sum" problem is a commonly encountered problem in computer -science and data analysis.the Three Sum problem has practical applications -in various fields, including data analysis, optimization, algorithmic research, -pattern recognition, and cryptography. Its versatility makes it a valuable problem -for both theoretical analysis and real-world problem-solving. - +https://en.wikipedia.org/wiki/3SUM """ @@ -16,14 +11,12 @@ def three_sum(nums: list[int]) -> list[list[int]]: nums: A sorted list of integers. Returns: - A list of lists containing unique triplets that sum up to zero. - - Example: - >>> three_sum([-1, 0, 1, 2, -1, -4]) - [[-1, -1, 2], [-1, 0, 1]] - >>> three_sum([1, 2, 3, 4]) - [] + A list of lists containing unique triplets that sum up to zero. + >>> three_sum([-1, 0, 1, 2, -1, -4]) + [[-1, -1, 2], [-1, 0, 1]] + >>> three_sum([1, 2, 3, 4]) + [] """ nums.sort() ans = [] @@ -48,7 +41,6 @@ def three_sum(nums: list[int]) -> list[list[int]]: return ans -# Run the doctests if __name__ == "__main__": import doctest