diff --git a/src/my_project/interviews/top_150_questions_round_13/two_sum.py b/src/my_project/interviews/top_150_questions_round_13/two_sum.py new file mode 100644 index 00000000..7b22ba02 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_13/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 + \ No newline at end of file diff --git a/tests/test_150_questions_round_13/test_two_sum_round_13.py b/tests/test_150_questions_round_13/test_two_sum_round_13.py new file mode 100644 index 00000000..2eb551c2 --- /dev/null +++ b/tests/test_150_questions_round_13/test_two_sum_round_13.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_13\ +.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