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..dc28488b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,18 @@ +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..cf306d3e --- /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_80_average_of_levels_in_binary_tree.py b/src/my_project/interviews/top_150_questions_round_22/ex_80_average_of_levels_in_binary_tree.py new file mode 100644 index 00000000..3c87bd77 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_80_average_of_levels_in_binary_tree.py @@ -0,0 +1,31 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +from collections import deque + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: + if not root: + return [] + + q = deque() + q.append(root) + answer = list() + + + while q: + qlen = len(q) + row = 0 + for i in range(qlen): + root: Optional[TreeNode] = q.popleft() + row += root.val + if root.left: q.append(root.left) + if root.right: q.append(root.right) + answer.append(row/qlen) + + return answer \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_80_average_of_levels_in_binary_tree.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_80_average_of_levels_in_binary_tree.ts new file mode 100644 index 00000000..23ae0b79 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_80_average_of_levels_in_binary_tree.ts @@ -0,0 +1,28 @@ +import { TreeNode } from './TreeNode'; + +function averageOfLevels(root: TreeNode | null): number[] { + if (!root) { + return []; + } + + const queue: TreeNode[] = []; + queue.push(root); + const answer: number[] = []; + + while (queue.length > 0) { + const qlen = queue.length; + let row = 0; + + for (let i = 0; i < qlen; i++) { + const node = queue.shift()!; + row += node.val; + + if (node.left) queue.push(node.left); + if (node.right) queue.push(node.right); + } + + answer.push(row / qlen); + } + + return answer; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_80_average_of_levels_in_binary_tree_round_22.py b/tests/test_150_questions_round_22/test_80_average_of_levels_in_binary_tree_round_22.py new file mode 100644 index 00000000..11667768 --- /dev/null +++ b/tests/test_150_questions_round_22/test_80_average_of_levels_in_binary_tree_round_22.py @@ -0,0 +1,78 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_80_average_of_levels_in_binary_tree import Solution, TreeNode + +class AverageOfLevelsInBinaryTreeTestCase(unittest.TestCase): + + def create_binary_tree(self, values): + """ + Helper function to create a binary tree from a list of values (level-order). + + :param values: List of node values (None represents null nodes) + :return: Root of the binary tree + """ + if not values: + return None + + root = TreeNode(values[0]) + queue = [root] + i = 1 + + while queue and i < len(values): + node = queue.pop(0) + + if i < len(values) and values[i] is not None: + node.left = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root + + def test_example_1(self): + # Example 1: Input: root = [3,9,20,null,null,15,7] + # Output: [3.00000,14.50000,11.00000] + # Explanation: The average value of nodes on level 0 is 3, + # on level 1 is 14.5, and on level 2 is 11. + solution = Solution() + root = self.create_binary_tree([3, 9, 20, None, None, 15, 7]) + result = solution.averageOfLevels(root) + expected = [3.00000, 14.50000, 11.00000] + self.assertEqual(len(result), len(expected)) + for i in range(len(result)): + self.assertAlmostEqual(result[i], expected[i], places=5) + + def test_example_2(self): + # Example 2: Input: root = [3,9,20,15,7] + # Output: [3.00000,14.50000,11.00000] + solution = Solution() + root = self.create_binary_tree([3, 9, 20, 15, 7]) + result = solution.averageOfLevels(root) + expected = [3.00000, 14.50000, 11.00000] + self.assertEqual(len(result), len(expected)) + for i in range(len(result)): + self.assertAlmostEqual(result[i], expected[i], places=5) + + def test_empty_tree(self): + # Test with empty tree + # Output: [] + solution = Solution() + root = self.create_binary_tree([]) + result = solution.averageOfLevels(root) + self.assertEqual(result, []) + + def test_single_node(self): + # Test with single node + # Input: root = [5] + # Output: [5.00000] + solution = Solution() + root = self.create_binary_tree([5]) + result = solution.averageOfLevels(root) + expected = [5.00000] + self.assertEqual(len(result), len(expected)) + for i in range(len(result)): + self.assertAlmostEqual(result[i], expected[i], places=5) \ No newline at end of file