From d751eb5e0a29ad9b7ce64193b789288644675b0c Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 26 Jul 2025 04:28:33 -0600 Subject: [PATCH] adding updates --- .../top_150_questions_round_18/two_sum.py | 16 ++++++++++++++++ .../test_two_sum_round_18.py | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_18/two_sum.py create mode 100644 tests/test_150_questions_round_18/test_two_sum_round_18.py diff --git a/src/my_project/interviews/top_150_questions_round_18/two_sum.py b/src/my_project/interviews/top_150_questions_round_18/two_sum.py new file mode 100644 index 00000000..d938c30a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_18/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 v not in dic_answer: + dic_answer[target - v] = k + else: + return [dic_answer[v], k] + + return -1 diff --git a/tests/test_150_questions_round_18/test_two_sum_round_18.py b/tests/test_150_questions_round_18/test_two_sum_round_18.py new file mode 100644 index 00000000..15860a5c --- /dev/null +++ b/tests/test_150_questions_round_18/test_two_sum_round_18.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_18\ +.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