From 545f88ff03d1ce063314e31e87ac185ef456e858 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 6 Apr 2026 04:25:24 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_2.py | 21 ------------------ .../common_algos/two_sum_round_3.py | 21 ------------------ .../common_algos/two_sum_round_4.py | 21 ------------------ .../common_algos/two_sum_round_5.py | 21 ------------------ .../common_algos/two_sum_round_6.py | 21 ------------------ .../common_algos/two_sum_round_7.py | 21 ------------------ .../common_algos/valid_palindrome_round_2.py | 22 ------------------- .../common_algos/valid_palindrome_round_3.py | 22 ------------------- .../common_algos/valid_palindrome_round_4.py | 22 ------------------- .../common_algos/valid_palindrome_round_5.py | 22 ------------------- .../common_algos/valid_palindrome_round_6.py | 22 ------------------- .../common_algos/valid_palindrome_round_7.py | 22 ------------------- .../ex_121_reverse_bits.py | 6 +++++ .../ex_121_reverse_bits.ts | 8 +++++++ .../test_121_reverse_bits_round_22.py | 11 ++++++++++ 15 files changed, 25 insertions(+), 258 deletions(-) delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_121_reverse_bits.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_121_reverse_bits.ts create mode 100644 tests/test_150_questions_round_22/test_121_reverse_bits_round_22.py 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 deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_3.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_6.py +++ /dev/null @@ -1,21 +0,0 @@ -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/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py deleted file mode 100644 index c7f907c0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py +++ /dev/null @@ -1,21 +0,0 @@ -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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_3.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py +++ /dev/null @@ -1,22 +0,0 @@ -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/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py +++ /dev/null @@ -1,22 +0,0 @@ -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/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_6.py +++ /dev/null @@ -1,22 +0,0 @@ -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/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 deleted file mode 100644 index fa21021b..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py +++ /dev/null @@ -1,22 +0,0 @@ -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_121_reverse_bits.py b/src/my_project/interviews/top_150_questions_round_22/ex_121_reverse_bits.py new file mode 100644 index 00000000..e79d85e3 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_121_reverse_bits.py @@ -0,0 +1,6 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def reverseBits(self, n: int) -> int: + return int((('{0:032b}'.format(n))[::-1]),2) \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_121_reverse_bits.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_121_reverse_bits.ts new file mode 100644 index 00000000..017c92b4 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_121_reverse_bits.ts @@ -0,0 +1,8 @@ +function reverseBits(n: number): number { + let result = 0; + for (let i = 0; i < 32; i++) { + result = (result << 1) | (n & 1); + n >>>= 1; + } + return result >>> 0; +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_121_reverse_bits_round_22.py b/tests/test_150_questions_round_22/test_121_reverse_bits_round_22.py new file mode 100644 index 00000000..e0625fd0 --- /dev/null +++ b/tests/test_150_questions_round_22/test_121_reverse_bits_round_22.py @@ -0,0 +1,11 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_121_reverse_bits import Solution + +class ReverseBitsTestCase(unittest.TestCase): + + def test_reverse_bits(self): + solution = Solution() + output = solution.reverseBits(2) + target = 1073741824 + self.assertEqual(output, target)