From 82a31db67f52db63cb75130f6f3a646618df290a Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 17 May 2025 04:31:00 -0600 Subject: [PATCH] adding algo --- .../top_150_questions_round_16/two_sum.py | 17 +++++++++++++++++ .../test_two_sum_round_16.py | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_16/two_sum.py create mode 100644 tests/test_150_questions_round_16/test_two_sum_round_16.py diff --git a/src/my_project/interviews/top_150_questions_round_16/two_sum.py b/src/my_project/interviews/top_150_questions_round_16/two_sum.py new file mode 100644 index 00000000..b1a15cf0 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_16/two_sum.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + dic_answer = dict() + + for k, v in enumerate(nums): + + + if v in dic_answer: + return [dic_answer[v], k] + else: + dic_answer[target - v] = k + + return -1 diff --git a/tests/test_150_questions_round_16/test_two_sum_round_16.py b/tests/test_150_questions_round_16/test_two_sum_round_16.py new file mode 100644 index 00000000..4f087f15 --- /dev/null +++ b/tests/test_150_questions_round_16/test_two_sum_round_16.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_16\ +.two_sum import Solution + + +class TwoSumTestCase(unittest.TestCase): + + def test_is_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=9) + target = [0,1] + for k, v in enumerate(target): + self.assertEqual(v, output[k]) + + def test_is_no_two_sum(self): + solution = Solution() + output = solution.twoSum(nums=[2,7,11,15], target=0) + target = -1 + self.assertEqual(output, target) \ No newline at end of file