Skip to content

Two-Sum to match target #12883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions interview-bootcamp/Two-Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def two_sum(nums: list[int], target: int) -> tuple[int, int] | None:

Check failure on line 1 in interview-bootcamp/Two-Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

interview-bootcamp/Two-Sum.py:1:1: INP001 File `interview-bootcamp/Two-Sum.py` is part of an implicit namespace package. Add an `__init__.py`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file interview-bootcamp/Two-Sum.py, please provide doctest for the function two_sum

"""
Find two numbers in the list 'nums' that add up to 'target'.

:param nums: List of integers
:param target: Integer target sum
:return: Tuple of the two numbers that add up to target, or None if no such pair exists

Check failure on line 7 in interview-bootcamp/Two-Sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

interview-bootcamp/Two-Sum.py:7:89: E501 Line too long (91 > 88)
"""
num_set = set()

for num in nums:
complement = target - num
if complement in num_set:
return (complement, num)
num_set.add(num)

return None


if __name__ == "__main__":
print(two_sum([2, 7, 11, 15], 9))
Loading