From 622d9cb245e5cc1cd225b5b21c192f4b664afbdc Mon Sep 17 00:00:00 2001 From: Sarita Rana Date: Fri, 19 Jul 2024 14:45:16 -0400 Subject: [PATCH] DSA Assignment 1 --- 02_activities/assignments/assignment_1.ipynb | 244 +++++++++++++++++-- 1 file changed, 218 insertions(+), 26 deletions(-) diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index d4ba17c..1e8ba85 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -21,11 +21,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 202, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ - "print((hash('your first name') % 3) + 1)" + "print((hash('sarita') % 3) + 1)" ] }, { @@ -72,9 +80,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 203, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block (2850872121.py, line 8)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[203], line 8\u001b[1;36m\u001b[0m\n\u001b[1;33m # TODO\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n" + ] + } + ], "source": [ "# Definition for a binary tree node.\n", "# class TreeNode(object):\n", @@ -82,10 +99,94 @@ "# self.val = val\n", "# self.left = left\n", "# self.right = right\n", - "def is_symmetric(root: TreeNode) -> int:\n", + "def is_duplicate(root: TreeNode) -> int:\n", " # TODO" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 195, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Example1\n", + "root = TreeNode(1)\n", + "root.left = TreeNode(2)\n", + "root.right = TreeNode(2)\n", + "root.left.left = TreeNode(3)\n", + "root.left.right = TreeNode(5)\n", + "root.right.left = TreeNode(6)\n", + "root.right.right = TreeNode(7)\n", + "# print_in_order(root)\n", + "is_duplicate(root)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 196, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Example2\n", + "root = TreeNode(1)\n", + "root.left = TreeNode(10)\n", + "root.right = TreeNode(2)\n", + "root.left.left = TreeNode(3)\n", + "root.left.right = TreeNode(10)\n", + "root.right.left = TreeNode(12)\n", + "root.right.right = TreeNode(12)\n", + "# print_in_order(root)\n", + "is_duplicate(root)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 197, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Example3\n", + "root = TreeNode(10)\n", + "root.left = TreeNode(9)\n", + "root.right = TreeNode(7)\n", + "root.left.left = TreeNode(8)\n", + "# print_in_order(root)\n", + "is_duplicate(root)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -193,12 +294,13 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "We need to traverse a binary tree and find any duplicate node values. \n", + " In case of multiple duplicates, the duplicate values closest to the root(Would be the first found duplicate in BFS) should be returned.\n", + "So, essentially we need to perform the breadth-first serach and return the first found duplicate value" ] }, { @@ -213,9 +315,62 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 198, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your answer here\n", + "# Example 1 Input: root = [10, 7, 2, 7, 3, 5, 2], output = 2\n", + "root = TreeNode(4)\n", + "root.left = TreeNode(7)\n", + "root.right = TreeNode(2)\n", + "root.left.left = TreeNode(11)\n", + "root.left.right = TreeNode(12)\n", + "root.right.left = TreeNode(5)\n", + "root.right.left.right = TreeNode(2)\n", + "root.right.left.left = TreeNode(14)\n", + "# print_in_order(root)\n", + "is_duplicate(root)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 199, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "# Example 2 Input: root = [1, 5, 7, 2, 2, 3, 7] Output =10\n", + "root = TreeNode(10)\n", + "root.left = TreeNode(7)\n", + "root.right = TreeNode(2)\n", + "root.left.left = TreeNode(1)\n", + "root.left.right = TreeNode(3)\n", + "root.right.left = TreeNode(5)\n", + "root.right.right = TreeNode(18)\n", + "root.right.left.left = TreeNode(12)\n", + "root.right.right.left = TreeNode(10)\n", + "# print_in_order(root)\n", + "is_duplicate(root)" ] }, { @@ -232,7 +387,30 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# Definition for a binary tree node.\n", + "class TreeNode(object):\n", + " def __init__(self, val = 0, left = None, right = None):\n", + " self.val = val\n", + " self.left = left\n", + " self.right = right\n", + "\n", + "def is_duplicate(root: TreeNode) -> int:\n", + " if not root:\n", + " return -1\n", + " queue = deque([root])\n", + " seen_values = set() \n", + " while queue:\n", + " node = queue.popleft()\n", + " if node.val in seen_values:\n", + " # if value is seen already return as duplicate \n", + " return node.val\n", + " seen_values.add(node.val)\n", + " if node.left:\n", + " queue.append(node.left)\n", + " if node.right:\n", + " queue.append(node.right)\n", + " return -1" ] }, { @@ -244,12 +422,12 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "While traversing the nodes, we maintain a set of unique values that we come across. If we encounter a duplicate, we return that value. In case it is a new value, we add it to the set.\n", + "The deque function helps us to create a queue which we then access from the left, to process the node values sequentially." ] }, { @@ -261,12 +439,13 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "Time complexity - In our solution code, each node is visited once , creating a time complexity of O(n)\n", + "\n", + "Space Complexity - The worst case for a highly skewed tree would have a space complexity of O(n)" ] }, { @@ -278,12 +457,25 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "I was thinking of using a recursive function\n", + "\n", + "The function would need two parameters, the node value and the seen list to keep track of the already visited values\n", + "\n", + "Under a function is_duplicate, do below:\n", + "\n", + "create an empty set to store unique values\n", + "\n", + "Evaluate the first node\n", + "if the node value is encountered the first time, add it to the set\n", + "otherwise, when encountering a value that already exists in set, return the value as a duplicate(and exit the function)\n", + "\n", + "continue evaluating the left and right branches and call the is_duplicate function recursively with the left node value and the set in it's current state\n", + "\n", + "\n" ] }, { @@ -345,7 +537,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.9.15" } }, "nbformat": 4,