diff --git a/src/my_project/interviews/top_150_questions_round_20/two_sum.py b/src/my_project/interviews/top_150_questions_round_20/two_sum.py new file mode 100644 index 00000000..0637b46e --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/two_sum.py @@ -0,0 +1,16 @@ +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 not v in dic_answer: + dic_answer[target - v] = k + else: + return [dic_answer[v], k] + + return -1 \ No newline at end of file diff --git a/tests/test_150_questions_round_20/test_two_sum_round_20.py b/tests/test_150_questions_round_20/test_two_sum_round_20.py new file mode 100644 index 00000000..7ba73b96 --- /dev/null +++ b/tests/test_150_questions_round_20/test_two_sum_round_20.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.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