From 24768210498499ea961fe4379ea3df2263813fd6 Mon Sep 17 00:00:00 2001 From: Johan Sandoval Date: Tue, 21 Jul 2026 19:01:26 -0500 Subject: [PATCH] Finish problem 0001-two-sum --- problems/0001-two-sum/solution.py | 8 ++++++++ problems/0001-two-sum/solution_test.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 problems/0001-two-sum/solution.py create mode 100644 problems/0001-two-sum/solution_test.py diff --git a/problems/0001-two-sum/solution.py b/problems/0001-two-sum/solution.py new file mode 100644 index 0000000..938ccbb --- /dev/null +++ b/problems/0001-two-sum/solution.py @@ -0,0 +1,8 @@ +class Solution: + def twoSum(self, nums: list[int], target: int) -> list[int]: + hashmap = {} + for i, n in enumerate(nums): + if target - n in hashmap: + return [hashmap[target - n], i] + hashmap[n] = i + return [] diff --git a/problems/0001-two-sum/solution_test.py b/problems/0001-two-sum/solution_test.py new file mode 100644 index 0000000..5537572 --- /dev/null +++ b/problems/0001-two-sum/solution_test.py @@ -0,0 +1,14 @@ +import pytest +from solution import Solution + + +@pytest.mark.parametrize( + "nums,target,expected", + [ + ([2, 7, 11, 15], 9, [0, 1]), + ([3, 2, 4], 6, [1, 2]), + ([3, 3], 6, [0, 1]), + ], +) +def test_two_sum(nums, target, expected): + assert Solution().twoSum(nums, target) == expected