diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.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_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.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_122_number_of_1_bits.py b/src/my_project/interviews/top_150_questions_round_22/ex_122_number_of_1_bits.py new file mode 100644 index 00000000..eafdab14 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_122_number_of_1_bits.py @@ -0,0 +1,6 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def hammingWeight(self, n: int) -> int: + return bin(n).count('1') diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_123_single_number.py b/src/my_project/interviews/top_150_questions_round_22/ex_123_single_number.py new file mode 100644 index 00000000..6751e0d3 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_123_single_number.py @@ -0,0 +1,12 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def singleNumber(self, nums: List[int]) -> int: + + answer = 0 + + for num in nums: + answer ^= num + + return answer \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_122_number_of_1_bits.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_122_number_of_1_bits.ts new file mode 100644 index 00000000..e6e7e8d4 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_122_number_of_1_bits.ts @@ -0,0 +1,8 @@ +function hammingWeight(n: number): number { + let count = 0; + while (n !== 0) { + count += n & 1; + n >>>= 1; + } + return count; +}; \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_123_single_number.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_123_single_number.ts new file mode 100644 index 00000000..abb40cc5 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_123_single_number.ts @@ -0,0 +1,3 @@ +function singleNumber(nums: number[]): number { + return nums.reduce((acc, n) => acc ^ n, 0); +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_122_number_of_1_bits_round_22.py b/tests/test_150_questions_round_22/test_122_number_of_1_bits_round_22.py new file mode 100644 index 00000000..bd101780 --- /dev/null +++ b/tests/test_150_questions_round_22/test_122_number_of_1_bits_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_122_number_of_1_bits import Solution + +class NumberOfOnesTestCase(unittest.TestCase): + + def test_number_of_ones(self): + solution = Solution() + output = solution.hammingWeight(n=11) + target = 3 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_123_single_number_round_22.py b/tests/test_150_questions_round_22/test_123_single_number_round_22.py new file mode 100644 index 00000000..41bc5176 --- /dev/null +++ b/tests/test_150_questions_round_22/test_123_single_number_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_123_single_number import Solution + +class SingleNumberTestCase(unittest.TestCase): + + def test_single_number(self): + solution = Solution() + output = solution.singleNumber(nums=[2,2,1]) + target = 1 + self.assertEqual(output, target) \ No newline at end of file