From 09840756e3fd95eb21c4ea1ced077aac89929953 Mon Sep 17 00:00:00 2001 From: spamegg Date: Wed, 23 Sep 2020 22:17:23 +0300 Subject: [PATCH 1/4] Add Python type hints and doctests to other/two_sum.py #2465 --- other/two_sum.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/other/two_sum.py b/other/two_sum.py index 8ac7a18ba9a4..7011ba049756 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -11,21 +11,36 @@ Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ +from typing import List -def twoSum(nums, target): +def two_sum(nums: List[int], target: int) -> List[int]: """ - :type nums: List[int] - :type target: int - :rtype: List[int] + >>> two_sum([2, 7, 11, 15], 9) + [0, 1] + >>> two_sum([15, 2, 11, 7], 13) + [1, 2] + >>> two_sum([2, 7, 11, 15], 17) + [0, 3] + >>> two_sum([7, 15, 11, 2], 18) + [0, 2] + >>> two_sum([2, 7, 11, 15], 26) + [2, 3] + >>> two_sum([2, 7, 11, 15], 8) + [] + >>> two_sum([3 * i for i in range(10)], 19) + [] """ chk_map = {} for index, val in enumerate(nums): compl = target - val if compl in chk_map: - indices = [chk_map[compl], index] - print(indices) - return [indices] - else: - chk_map[val] = index - return False + return [chk_map[compl], index] + chk_map[val] = index + return [] + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e3aa1b3a04f5d5336d401b0dbe083194980388fc Mon Sep 17 00:00:00 2001 From: spamegg <4255997+spamegg1@users.noreply.github.com> Date: Wed, 23 Sep 2020 22:42:05 +0300 Subject: [PATCH 2/4] Update other/two_sum.py Co-authored-by: Christian Clauss --- other/two_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/two_sum.py b/other/two_sum.py index 7011ba049756..e9b90563062d 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -11,7 +11,7 @@ Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ -from typing import List +from __future__ import annotations def two_sum(nums: List[int], target: int) -> List[int]: From 0bfd6b3343c4e7c264dcae2a2026ba2c64246256 Mon Sep 17 00:00:00 2001 From: spamegg <4255997+spamegg1@users.noreply.github.com> Date: Wed, 23 Sep 2020 22:43:01 +0300 Subject: [PATCH 3/4] Update other/two_sum.py Co-authored-by: Christian Clauss --- other/two_sum.py | 1 + 1 file changed, 1 insertion(+) diff --git a/other/two_sum.py b/other/two_sum.py index e9b90563062d..b6262819ffe7 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -44,3 +44,4 @@ def two_sum(nums: List[int], target: int) -> List[int]: import doctest doctest.testmod() + print(f"{two_sum([2, 7, 11, 15], 9) = }") From 4203f373d15a52aecf360a96994bc0477a0097e7 Mon Sep 17 00:00:00 2001 From: spamegg <4255997+spamegg1@users.noreply.github.com> Date: Wed, 23 Sep 2020 22:51:05 +0300 Subject: [PATCH 4/4] Update two_sum.py --- other/two_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/two_sum.py b/other/two_sum.py index b6262819ffe7..5209acbc7e44 100644 --- a/other/two_sum.py +++ b/other/two_sum.py @@ -14,7 +14,7 @@ from __future__ import annotations -def two_sum(nums: List[int], target: int) -> List[int]: +def two_sum(nums: list[int], target: int) -> list[int]: """ >>> two_sum([2, 7, 11, 15], 9) [0, 1]