diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.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_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.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_120_add_binary.py b/src/my_project/interviews/top_150_questions_round_22/ex_120_add_binary.py new file mode 100644 index 00000000..46931977 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_120_add_binary.py @@ -0,0 +1,7 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def addBinary(self, a: str, b: str) -> str: + + return bin(int(a,2) + int(b,2))[2:] \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_120_add_binary.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_120_add_binary.ts new file mode 100644 index 00000000..ade21066 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_120_add_binary.ts @@ -0,0 +1,18 @@ +function addBinary(a: string, b: string): string { + let i = a.length - 1; + let j = b.length - 1; + let carry = 0; + let result = ""; + + while (i >= 0 || j >= 0 || carry) { + const digitA = i >= 0 ? Number(a[i]) : 0; + const digitB = j >= 0 ? Number(b[j]) : 0; + const sum = digitA + digitB + carry; + result = (sum % 2) + result; + carry = Math.floor(sum / 2); + i--; + j--; + } + + return result; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_120_add_binary_round_22.py b/tests/test_150_questions_round_22/test_120_add_binary_round_22.py new file mode 100644 index 00000000..dd0ce1fc --- /dev/null +++ b/tests/test_150_questions_round_22/test_120_add_binary_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_120_add_binary import Solution + +class AddBinaryTestCase(unittest.TestCase): + + def test_add_binary(self): + solution = Solution() + output = solution.addBinary(a="11", b="1") + target = "100" + self.assertEqual(output, target) \ No newline at end of file