From 588bea5aa31761de89151e12a3cff64531d77c00 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 13 Apr 2026 04:27:46 -0600 Subject: [PATCH] adding sqrtx and pow x n algos --- .../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_129_sqrtx.py | 20 +++++++++++++ .../ex_130_pow_x_n.py | 14 +++++++++ .../top_150_questions_round_1/ex_129_sqrtx.ts | 29 +++++++++++++++++++ .../ex_130_pow_x_n.ts | 14 +++++++++ .../test_129_sqrtx_round_22.py | 17 +++++++++++ .../test_130_pow_x_n_round_22.py | 29 +++++++++++++++++++ 18 files changed, 123 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_129_sqrtx.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_130_pow_x_n.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_129_sqrtx.ts create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_130_pow_x_n.ts create mode 100644 tests/test_150_questions_round_22/test_129_sqrtx_round_22.py create mode 100644 tests/test_150_questions_round_22/test_130_pow_x_n_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_129_sqrtx.py b/src/my_project/interviews/top_150_questions_round_22/ex_129_sqrtx.py new file mode 100644 index 00000000..a51a465b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_129_sqrtx.py @@ -0,0 +1,20 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def mySqrt(self, x: int) -> int: + + left, right = 0, x + + while left <= right: + + mid = (left + right)//2 + + if mid ** 2 < x: + left = mid + 1 + elif mid ** 2 > x: + right = mid - 1 + else: + return mid + + return min(left, right) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_130_pow_x_n.py b/src/my_project/interviews/top_150_questions_round_22/ex_130_pow_x_n.py new file mode 100644 index 00000000..70b662d3 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_130_pow_x_n.py @@ -0,0 +1,14 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def myPow(self, x, n): + + if not n: + return 1 + if n < 1: + return self.myPow(1/x, -n) + if n % 2: + return x*self.myPow(x,n-1) + else: + return self.myPow(x**2,n//2) \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_129_sqrtx.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_129_sqrtx.ts new file mode 100644 index 00000000..b65bcff5 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_129_sqrtx.ts @@ -0,0 +1,29 @@ +function mySqrt(x: number): number { + // Edge cases + if (x === 0 || x === 1) { + return x; + } + + // Binary search for the square root + let left = 1; + let right = x; + let result = 0; + + while (left <= right) { + const mid = Math.floor(left + (right - left) / 2); + const square = mid * mid; + + if (square === x) { + return mid; + } else if (square < x) { + // Store the potential answer and search in the right half + result = mid; + left = mid + 1; + } else { + // square > x, search in the left half + right = mid - 1; + } + } + + return result; +}; \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_130_pow_x_n.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_130_pow_x_n.ts new file mode 100644 index 00000000..73f0d2fc --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_130_pow_x_n.ts @@ -0,0 +1,14 @@ +function myPow(x: number, n: number): number { + + if (!n){ + return 1 + } + if (n < 0){ + return 1/myPow(x,-n) + } + if (n%2 !== 0){ + return x * myPow(x,n-1) + } + return myPow(x*x,n/2) + +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_129_sqrtx_round_22.py b/tests/test_150_questions_round_22/test_129_sqrtx_round_22.py new file mode 100644 index 00000000..82f32c6b --- /dev/null +++ b/tests/test_150_questions_round_22/test_129_sqrtx_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_129_sqrtx import Solution + +class SqrtxTestCase(unittest.TestCase): + + def test_even_sqrtx(self): + solution = Solution() + output = solution.mySqrt(x=4) + target = 2 + self.assertEqual(output, target) + + def test_odd_sqrtx(self): + solution = Solution() + output = solution.mySqrt(x=8) + target = 2 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_130_pow_x_n_round_22.py b/tests/test_150_questions_round_22/test_130_pow_x_n_round_22.py new file mode 100644 index 00000000..01558584 --- /dev/null +++ b/tests/test_150_questions_round_22/test_130_pow_x_n_round_22.py @@ -0,0 +1,29 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_130_pow_x_n import Solution + +class PowxnTestCase(unittest.TestCase): + + def test_powxn_zero_power(self): + solution = Solution() + output = solution.myPow(x=2, n=0) + target = 1 + self.assertEqual(output, target) + + def test_powxn_negative_power(self): + solution = Solution() + output = solution.myPow(x=2, n=-1) + target = 0.5 + self.assertEqual(output, target) + + def test_powxn_odd_power(self): + solution = Solution() + output = solution.myPow(x=2, n=1) + target = 2 + self.assertEqual(output, target) + + def test_powxn_even_power(self): + solution = Solution() + output = solution.myPow(x=2, n=2) + target = 4 + self.assertEqual(output, target) \ No newline at end of file