From 5600eb48112713d291274598ae50fa034d37ad78 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 10 Apr 2026 04:28:49 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_5.py | 21 ++++++++++++++++++ .../common_algos/valid_palindrome_round_5.py | 22 +++++++++++++++++++ .../ex_126_palindrome_number.py | 9 ++++++++ .../ex_126_palindrome_number.ts | 11 ++++++++++ .../test_126_palindrome_number_round_22.py | 15 +++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_126_palindrome_number.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_126_palindrome_number.ts create mode 100644 tests/test_150_questions_round_22/test_126_palindrome_number_round_22.py 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 new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_5.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_5.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_5.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_126_palindrome_number.py b/src/my_project/interviews/top_150_questions_round_22/ex_126_palindrome_number.py new file mode 100644 index 00000000..80952ebd --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_126_palindrome_number.py @@ -0,0 +1,9 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def isPalindrome(self, x: int) -> bool: + + str_x = str(x) + + return str_x == str_x[::-1] \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_126_palindrome_number.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_126_palindrome_number.ts new file mode 100644 index 00000000..6506c3d0 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_126_palindrome_number.ts @@ -0,0 +1,11 @@ +function isPalindrome(x: number): boolean { + if (x < 0 || (x % 10 === 0 && x !== 0)) return false; + + let reversed = 0; + while (x > reversed) { + reversed = reversed * 10 + (x % 10); + x = Math.floor(x / 10); + } + + return x === reversed || x === Math.floor(reversed / 10); +}; \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_126_palindrome_number_round_22.py b/tests/test_150_questions_round_22/test_126_palindrome_number_round_22.py new file mode 100644 index 00000000..06a83872 --- /dev/null +++ b/tests/test_150_questions_round_22/test_126_palindrome_number_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_126_palindrome_number import Solution + +class PalindromeNumberTestCase(unittest.TestCase): + + def test_is_palindrome_number(self): + solution = Solution() + output = solution.isPalindrome(x=121) + self.assertTrue(output) + + def test_is_no_palindrome_number(self): + solution = Solution() + output = solution.isPalindrome(x=123) + self.assertFalse(output) \ No newline at end of file