Repository: TheAlgorithms/Python
Pull request: Add Python type hints and doctests to other/two_sum.py
Fixes #2465

### **Describe your change:**



* [ ] Add an algorithm?
* [x] Fix a bug or typo in an existing algorithm?
* [ ] Documentation change?

### **Checklist:**
* [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md).
* [x] This pull request is all my own work -- I have not plagiarized.
* [x] I know that pull requests will not be merged if they fail the automated tests.
* [x] This PR only changes one algorithm file.  To ease review, please open separate PRs for separate algorithms.
* [x] All new Python files are placed inside an existing directory.
* [x] All filenames are in all lowercase characters with no spaces or dashes.
* [x] All functions and variable names follow Python naming conventions.
* [x] All function parameters and return values are annotated with Python [type hints](https://docs.python.org/3/library/typing.html).
* [x] All functions have [doctests](https://docs.python.org/3/library/doctest.html) that pass the automated testing.
* [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
* [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


Problem statement:
Add Python type hints and doctests to other/two_sum.py
@tonydelanuez Would you be willing to add Python type hints and doctests to [`other/two_sum.py`](../blob/master/other/two_sum.py)?  The function comments say that it will return a `list[int]` but instead it either returns a `list[list[int]]` or `False`.

Hints from issue discussion:

Commit message: Add Python type hints and doctests to other/two_sum.py

#2465

Patch to review:
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()


Code review comments:
diff_hunk: @@ -11,21 +11,36 @@
 Because nums[0] + nums[1] = 2 + 7 = 9,
 return [0, 1].
 """
+from typing import List
path: other/two_sum.py
text: @user1:
```suggestion
from __future__ import annotations
```

diff_hunk: @@ -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]:
path: other/two_sum.py
text: @user1:
```suggestion
def two_sum(nums: list[int], target: int) -> list[int]:
```

@user1:
Travis tests fail without this change.

@author:
I clicked "Commit suggestion" but I guess it didn't work.

@author:
I committed it manually below, still waiting for Travis to kick in.

@user1:
You can `Add suggestion to batch` on all suggestions that then they will commit all at once.

@author:
Oh! Didn't know that! Thanks. Github is so confusing! It should be good now.