From 7a23a43efeb39554257e4f426b882e903ab6d17c Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 8 Apr 2026 04:27:29 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_3.py | 21 ++++++++++++++++++ .../common_algos/valid_palindrome_round_3.py | 22 +++++++++++++++++++ .../ex_124_single_number_ii.py | 9 ++++++++ .../ex_124_single_number_ii.ts | 8 +++++++ .../test_124_single_number_ii_round_22.py | 22 +++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_124_single_number_ii.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_124_single_number_ii.ts create mode 100644 tests/test_150_questions_round_22/test_124_single_number_ii_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py @@ -0,0 +1,21 @@ +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]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_124_single_number_ii.py b/src/my_project/interviews/top_150_questions_round_22/ex_124_single_number_ii.py new file mode 100644 index 00000000..36f22838 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_124_single_number_ii.py @@ -0,0 +1,9 @@ +from typing import List + +class Solution: + def singleNumber(self, nums: List[int]) -> int: + ones, twos = 0, 0 + for n in nums: + ones = (ones ^ n) & ~twos + twos = (twos ^ n) & ~ones + return ones \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_124_single_number_ii.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_124_single_number_ii.ts new file mode 100644 index 00000000..545c5ffc --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_124_single_number_ii.ts @@ -0,0 +1,8 @@ +function singleNumberII(nums: number[]): number { + let ones = 0, twos = 0; + for (const n of nums) { + ones = (ones ^ n) & ~twos; + twos = (twos ^ n) & ~ones; + } + return ones; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_124_single_number_ii_round_22.py b/tests/test_150_questions_round_22/test_124_single_number_ii_round_22.py new file mode 100644 index 00000000..d76150e3 --- /dev/null +++ b/tests/test_150_questions_round_22/test_124_single_number_ii_round_22.py @@ -0,0 +1,22 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_124_single_number_ii import Solution + +class SingleNumberIITestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + self.assertEqual(self.solution.singleNumber([2, 2, 3, 2]), 3) + + def test_example_2(self): + self.assertEqual(self.solution.singleNumber([0, 1, 0, 1, 0, 1, 99]), 99) + + def test_single_element(self): + self.assertEqual(self.solution.singleNumber([1]), 1) + + def test_negative_numbers(self): + self.assertEqual(self.solution.singleNumber([-2, -2, 1, -2]), 1) + + def test_zero_is_single(self): + self.assertEqual(self.solution.singleNumber([5, 5, 5, 0]), 0) \ No newline at end of file