Skip to content

Commit d48e8a3

Browse files
committed
Two-Sum to match target
1 parent 7a0fee4 commit d48e8a3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

interview-bootcamp/Two-Sum.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def to_sum(nums, target):
2+
"""
3+
This function finds two numbers in the list 'nums' that add up to 'target'.
4+
5+
:param nums: List of integers
6+
:param target: Integer target sum
7+
:return: Tuple of the two numbers that add up to target, or None if no such pair exists
8+
"""
9+
num_set = set()
10+
11+
for num in nums:
12+
complement = target - num
13+
if complement in num_set:
14+
return (complement, num)
15+
num_set.add(num)
16+
17+
return None
18+
19+
if __name__ == "__main__":
20+
print(to_sum([2, 7, 11, 15], 9))

0 commit comments

Comments
 (0)