From 22109cf0c737348208e0c0f78641d45c2cb22006 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 17 Nov 2025 04:22:45 -0600 Subject: [PATCH] adding algo --- .../common_algos/two_sum_round_9.py | 16 ++++++++++ .../common_algos/valid_palindrome_round_9.py | 23 +++++++++++++ .../round_3/optimal_partition_string.py | 16 ++++++++++ .../reorder_data_in_log_file_round_1.py | 10 +++--- .../symmetric_tree.py | 32 +++++++++++++++++++ .../test_symmetric_tree_round_21.py | 15 +++++++++ 6 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/optimal_partition_string.py rename src/my_project/interviews/amazon_high_frequency_23/{round_2 => round_3}/reorder_data_in_log_file_round_1.py (54%) create mode 100644 src/my_project/interviews/top_150_questions_round_21/symmetric_tree.py create mode 100644 tests/test_150_questions_round_21/test_symmetric_tree_round_21.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_9.py @@ -0,0 +1,16 @@ +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 [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py new file mode 100644 index 00000000..1ed43720 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_9.py @@ -0,0 +1,23 @@ +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='[^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 \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/optimal_partition_string.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/optimal_partition_string.py new file mode 100644 index 00000000..bee30b15 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/optimal_partition_string.py @@ -0,0 +1,16 @@ +class Solution: + def partitionString(self, s: str) -> int: + + last_pos = [0] * 26 + partitions = 0 + last_end = 0 + + for i in range(len(s)): + + if last_pos[ord(s[i]) - ord('a')] >= last_end: + partitions += 1 + last_end = i + 1 + + last_pos[ord(s[i]) - ord('a')] = i + 1 + + return partitions \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/reorder_data_in_log_file_round_1.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/reorder_data_in_log_file_round_1.py similarity index 54% rename from src/my_project/interviews/amazon_high_frequency_23/round_2/reorder_data_in_log_file_round_1.py rename to src/my_project/interviews/amazon_high_frequency_23/round_3/reorder_data_in_log_file_round_1.py index 8d07d57d..616e8bf4 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/reorder_data_in_log_file_round_1.py +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/reorder_data_in_log_file_round_1.py @@ -7,10 +7,12 @@ def reorderLogFiles(self, logs: List[str]) -> List[str]: digit_logs = [] for log in logs: - _id, rest = log.split(' ', 1) + id_, rest = log.split(' ', 1) if rest[0].isdigit(): digit_logs.append(log) else: - letter_logs.append((rest, _id, log)) - letter_logs.sort(key=lambda x: (x[0],x[1])) - return [orig for _,_,orig in letter_logs] + digit_logs \ No newline at end of file + letter_logs.append((rest,id_,log)) + + # sort by content first, then identifier + letter_logs.sort(key=lambda x: (x[0], x[1])) + return [orig for _, _, orig in letter_logs] + digit_logs \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/symmetric_tree.py b/src/my_project/interviews/top_150_questions_round_21/symmetric_tree.py new file mode 100644 index 00000000..99061a0d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/symmetric_tree.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + + def isSymmetric(self, root: TreeNode) -> bool: + return self.check_mirror(root1=root, root2=root) + + def check_mirror(self, root1: TreeNode, root2: TreeNode): + + if root1 is None and root2 is None: + return True + elif root1 is not None and root2 is not None: + try: + root1.val + root2.val + except: + return False + + if root1.val != root2.val: + return False + else: + return self.check_mirror(root1.left, root2.right) \ + and self.check_mirror(root1.right, root2.left) + else: + return False \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_symmetric_tree_round_21.py b/tests/test_150_questions_round_21/test_symmetric_tree_round_21.py new file mode 100644 index 00000000..f0dd15d3 --- /dev/null +++ b/tests/test_150_questions_round_21/test_symmetric_tree_round_21.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.symmetric_tree import Solution, TreeNode + +class SymmetricTreeTestCase(unittest.TestCase): + + def test_is_symmetric(self): + solution = Solution() + output = solution.isSymmetric(TreeNode(1, TreeNode(7), TreeNode(7))) + self.assertTrue(output) + + def test_is_no_symmetric(self): + solution = Solution() + output = solution.isSymmetric(TreeNode(1, TreeNode(2), TreeNode(3))) + self.assertFalse(output) \ No newline at end of file