From 31be71988a612f9f2cdabd1bc23e0a48af94c7d9 Mon Sep 17 00:00:00 2001 From: Michael Chebotarov Date: Tue, 27 Oct 2026 20:00:06 -0400 Subject: [PATCH 1/2] working on assignment 2 --- 02_activities/assignments/assignment_2.ipynb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 1a0e6b8f..adaee980 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -29,9 +29,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your assigned problem is: 3\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -39,7 +47,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 = \"Mike\"\n", "result = hash_to_range(input_string)\n", "print(f\"Your assigned problem is: {result}\")" ] @@ -383,7 +391,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "algos-env (3.11.14)", "language": "python", "name": "python3" }, @@ -397,7 +405,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.14" } }, "nbformat": 4, From 51f388e18b62415f214c6f899d41f8629f860fb0 Mon Sep 17 00:00:00 2001 From: Michael Chebotarov Date: Wed, 28 Oct 2026 20:01:42 -0400 Subject: [PATCH 2/2] Finished assignment 2 --- 02_activities/assignments/assignment_2.ipynb | 127 ++++++++++++++++--- 1 file changed, 108 insertions(+), 19 deletions(-) diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index adaee980..b5ed783d 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -36,7 +36,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Your assigned problem is: 3\n" + "Your assigned problem is: 1\n" ] } ], @@ -47,7 +47,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 = \"Mike\"\n", + "input_string = \"michael\"\n", "result = hash_to_range(input_string)\n", "print(f\"Your assigned problem is: {result}\")" ] @@ -92,19 +92,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], "source": [ + "from collections import deque\n", + "\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", + "\n", "def is_duplicate(root: TreeNode) -> int:\n", - " # Code your solution here #\n", - " # TODO" + " if root is None:\n", + " return -1\n", + "\n", + " visitedNodes = set()\n", + " queue = deque()\n", + " queue.append(root)\n", + "\n", + " while queue:\n", + " node = queue.popleft()\n", + "\n", + " if node.val in visitedNodes:\n", + " return node.val\n", + " visitedNodes.add(node.val)\n", + "\n", + " if node.left is not None:\n", + " queue.append(node.left)\n", + " if node.right is not None:\n", + " queue.append(node.right)\n", + "\n", + " return -1\n", + "\n", + "[1, 10, 2, 3, 10, 12, 12]\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", + "root\n", + "print(is_duplicate(root))\n", + " " ] }, { @@ -221,7 +263,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The problem asks to check if the input bracket sequence is in the correct order such that there's a closing bracket of the same type\n", + "# that matches an opening bracket of the same type and the most recently open bracket must close first." ] }, { @@ -238,7 +281,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Example:\n", + "# input: s= \"(({[{]}))\"\n", + "# Ouput: False" ] }, { @@ -254,7 +299,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# My partner created this example Example 6: • Input: s = \"({[})\" • Output: False\n", + "#\n", + "#There is an open '('. followed by a '{' bracket, and then '['. Immediately after there is a '}' which is a closing bracket and it does\n", + "# not match the immediately open bracket of '[' before that, so it returns False" ] }, { @@ -271,7 +319,23 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "def is_valid_brackets(s: str) -> bool:\n", + " stack=[]\n", + " mapping = {\")\":\"(\",\"]\":\"[\",\"}\":\"{\"}\n", + "\n", + " for char in s:\n", + " if char in mapping:\n", + " if not stack:\n", + " return False\n", + " top_element = stack.pop()\n", + " if top_element != mapping[char]:\n", + " return False\n", + "\n", + " else:\n", + " stack.append(char)\n", + "\n", + " return len(stack) == 0" ] }, { @@ -287,7 +351,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# The solution checks each character in the input string. It will push to the stack if it's not in the mapping of closing brackets. \n", + "# If it is in the mapping of closing brackets, then it will check if the top element of the stack matches the value of the key of\n", + "# the closing bracket. If it does, it will continue checking the next characters, otherwise it will return false. If a closing bracket \n", + "# appears and the stack is empty, it will also return false as there is no matching open bracket.\n", + "# This works as the stack's top element always holds the most recent unmatched opening bracket that needs to be matched by a closing bracket.\n", + "# After the string was processed, the stack must be empty for the input to be considered valid." ] }, { @@ -303,7 +372,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# It uses both O(n) time and O(n) space. For time, it will be O(n) because at worst it needs to check each character in the input \n", + "# string once.\n", + "# For O(n) space, because there will be at worst case O(n) elements in the stack if all characters are open brackets.\n", + "\n", + "# Yes, they correctly mentioned both the time and space complexity for this solution." ] }, { @@ -319,7 +392,19 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# They could combine the following lines into a single condition\n", + "# if not stack:\n", + "# return False\n", + "# top_element = stack.pop()\n", + "# if top_element != mapping[char]:\n", + "# return False\n", + "\n", + "# They could combine it into:\n", + "\n", + "# if not stack or stack.pop() != mapping[char]:\n", + "# return False\n", + "\n", + "# It shorterns the code, but doesn't change the its time or space complexity." ] }, { @@ -339,7 +424,11 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# For Assignment 1 & 2, I used a digital notebook to draw diagrams and visualize the problem. It helped to understand what was being\n", + "# asked. After that, I tried to create pseudocode to outline a solution before trying to code it. When I coded it, I used print \n", + "# statements to debug and verify my logic. Then I used test data to check my work.\n", + "\n", + "# For assignment 2, I had to check documentation to understand how to define queues and how to use them in python. " ] }, {