Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 119 additions & 22 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your assigned problem is: 1\n"
]
}
],
"source": [
"import hashlib\n",
"\n",
"def hash_to_range(input_string: str) -> int:\n",
" 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 = \"michael\"\n",
"result = hash_to_range(input_string)\n",
"print(f\"Your assigned problem is: {result}\")"
]
Expand Down Expand Up @@ -84,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",
" "
]
},
{
Expand Down Expand Up @@ -213,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."
]
},
{
Expand All @@ -230,7 +281,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Example:\n",
"# input: s= \"(({[{]}))\"\n",
"# Ouput: False"
]
},
{
Expand All @@ -246,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"
]
},
{
Expand All @@ -263,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"
]
},
{
Expand All @@ -279,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."
]
},
{
Expand All @@ -295,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."
]
},
{
Expand All @@ -311,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."
]
},
{
Expand All @@ -331,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. "
]
},
{
Expand Down Expand Up @@ -383,7 +480,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env (3.11.14)",
"language": "python",
"name": "python3"
},
Expand All @@ -397,7 +494,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down