diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 26bb3864..b9b55520 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -27,9 +27,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -37,7 +45,7 @@ " hash_object = hashlib.sha256(input_string.encode())\n", " hash_int = int(hash_object.hexdigest(), 16)\n", " return (hash_int % 3) + 1\n", - "input_string = \"your_first_name_here\"\n", + "input_string = \"Isra\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -86,18 +94,114 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ + "from collections import deque\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", + "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", - " # TODO" + " if root is None: \n", + " return -1\n", + " found = set()\n", + " queue = deque([root])\n", + " while queue:\n", + " node = queue.popleft()\n", + " if node.val in found:\n", + " return node.val\n", + " found.add(node.val)\n", + " if node.left:\n", + " queue.append(node.left)\n", + " if node.right:\n", + " queue.append(node.right)\n", + " return -1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-1" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "root = TreeNode(10)\n", + "root.right = TreeNode(7)\n", + "root.left = TreeNode(9)\n", + "root.left.left = TreeNode(8)\n", + "is_duplicate(root)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "root = TreeNode(1)\n", + "root.right = TreeNode(2)\n", + "root.left = TreeNode(2)\n", + "root.left.left = TreeNode(3)\n", + "root.left.right = TreeNode(5)\n", + "root.right.right = TreeNode(6)\n", + "root.right.left = TreeNode(7)\n", + "is_duplicate(root)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "root = [1, 10, 2, 3, 10, 12, 12]\n", + "\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", + "is_duplicate(root)" ] }, { @@ -227,7 +331,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The problem is to check if the brackets are balanced or not, when given a string of brackets.\n", + "# The brackets include (), {}, and [].\n", + "# The function should return True if the brackets are balanced, and False otherwise.\n", + "# For example, \"({[]})\" is balanced, but \"({[})\" is not." ] }, { @@ -240,11 +347,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Example: ({[]})\n", + "# A dictornary or map is created to hold the opening and closing bracket values.\n", + "# An empty stack is created to hold the opening brackets as they are encountered in the string.\n", + "# The code will check the character in the string if it contains an open bracket value. \n", + "# If it does, it will be added to the stack.\n", + "# If it doesn't contain an open bracket value, it will check if contains a close bracket value.\n", + "# If it does, it will check if the stack is empty. If the stack is empty, it means there is no matching opening bracket for the closing bracket, so it will return False.\n", + "# If the stack is not empty, it will pop the top value from the stack and check if it matches the corresponding opening bracket for the closing bracket. If it doesn't match, it will return False.\n", + "\n", + "# Now for the example \"({[]})\":\n", + "# The first character is '(', opening bracket, so it will be added to the stack.\n", + "# The second character is '{', another opening bracket, so it will be added to the stack.\n", + "# The third character is '[', another opening bracket, so it will be added to the stack.\n", + "# The fourth character is ']', a closing bracket. The stack is not empty, the top value is popped from the stack, which is '['. It matches the opening bracket for ']' in the dictionary, so we continue.\n", + "# The fifth character is '}', another closing bracket. The stack is not empty, the top value is popped from the stack, which is '{'. It matches the opening bracket for '}' in the dictionary, so we continue.\n", + "# The sixth character is ')', another closing bracket. The stack is not empty, the top value is popped from the stack, which is '('. It matches the opening bracket for ')' in the dictionary, so we continue.\n", + "# At the end of the string, the stack is empty, which means all opening brackets have been matched with closing brackets, so the function returns True, indicating that the brackets are balanced." ] }, { @@ -257,11 +380,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "def is_valid_brackets(s: str) -> bool:\n", + " stack = []\n", + " bracket_map = {\")\": \"(\", \"}\": \"{\", \"]\": \"[\"}\n", + "\n", + " for ch in s:\n", + " if ch in bracket_map.values():\n", + " #add open brackets\n", + " stack.append(ch)\n", + " elif ch in bracket_map.keys():\n", + " #check if closed bracket is presceeded by its open bracket\n", + " if not stack or bracket_map[ch] != stack.pop():\n", + " return False\n", + " else:\n", + " return False\n", + " #When string is properly nested, stack is empty and returns True\n", + " return not stack" ] }, { @@ -274,11 +413,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The solution works because it uses a dictonary to map closing brackets to their corresponding open brackets. \n", + "# This makes it easy to check the closing and open brackets against each other. \n", + "# The if statements make the solution efficient by checking the characters in the string once and using a list to store the open brackets. \n" ] }, { @@ -291,11 +432,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The time complexity is O(n) because we iterate through the string once and n is the length of the string. \n", + "# The space complexity is O(n) in the worst case when all the brackets are opening brackets and it is less than O(n) in other cases. " ] }, { @@ -308,11 +450,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The solution is efficent and works well for checking balanced brackets in a string.\n", + "# Another way to approach the problem is to use set for opening brackets to reduce lookup time.\n", + "# instead of calling bracket_map.values() which is O(n), we can use a set which is O(1) for lookups." ] }, { @@ -334,11 +478,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "' Assignment 1 gave me the foundation understanding of data structure and algorithms. \\n It helped me understand how to approach problems with more efficency and look for optimal solutions instead of relying on one data structure \\n I can use a varity of data structure to solve problems in different ways. \\n The same problem can be solved using different data structures and algorithms and each will have its own pros and cons.\\n This assignment helped me see how others have approached problems in different ways\\n Being able to critique and observe my colleagues work help me understand different perspectives of solving problems. \\n Assignment 2 build on that knowledge and helped me apply those concepts in more complex problems.\\n I was able to understand the complex problems of Trees and their traversals.\\n I was able to not only implement the traversals but also understand their use cases and when to apply them.\\n'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your answer here" + "''' Assignment 1 gave me the foundation understanding of data structure and algorithms. \n", + " It helped me understand how to approach problems with more efficency and look for optimal solutions instead of relying on one data structure \n", + " I can use a varity of data structure to solve problems in different ways. \n", + " The same problem can be solved using different data structures and algorithms and each will have its own pros and cons.\n", + " This assignment helped me see how others have approached problems in different ways\n", + " Being able to critique and observe my colleagues work help me understand different perspectives of solving problems. \n", + " Assignment 2 build on that knowledge and helped me apply those concepts in more complex problems.\n", + " I was able to understand the complex problems of Trees and their traversals.\n", + " I was able to not only implement the traversals but also understand their use cases and when to apply them.\n", + "'''" ] }, { @@ -396,7 +560,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -410,7 +574,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.9.23" } }, "nbformat": 4,