diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.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_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.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_125_bitwise_and_of_numbers_range.py b/src/my_project/interviews/top_150_questions_round_22/ex_125_bitwise_and_of_numbers_range.py new file mode 100644 index 00000000..026070f0 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_125_bitwise_and_of_numbers_range.py @@ -0,0 +1,7 @@ +from typing import List + +class Solution: + def rangeBitwiseAnd(self, left: int, right: int) -> int: + while left < right: + right = right & (right - 1) + return right \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_125_bitwise_and_of_numbers_range.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_125_bitwise_and_of_numbers_range.ts new file mode 100644 index 00000000..fd91bfdb --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_125_bitwise_and_of_numbers_range.ts @@ -0,0 +1,6 @@ +function rangeBitwiseAnd(left: number, right: number): number { + while (left < right) { + right = right & (right - 1); + } + return right; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_125_bitwise_and_of_numbers_range_round_22.py b/tests/test_150_questions_round_22/test_125_bitwise_and_of_numbers_range_round_22.py new file mode 100644 index 00000000..8d14e0b9 --- /dev/null +++ b/tests/test_150_questions_round_22/test_125_bitwise_and_of_numbers_range_round_22.py @@ -0,0 +1,16 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_125_bitwise_and_of_numbers_range import Solution + +class BitwiseAndOfNumbersRangeTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + self.assertEqual(self.solution.rangeBitwiseAnd(5, 7), 4) + + def test_example_2(self): + self.assertEqual(self.solution.rangeBitwiseAnd(0, 0), 0) + + def test_example_3(self): + self.assertEqual(self.solution.rangeBitwiseAnd(1, 2147483647), 0) \ No newline at end of file