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
196 changes: 176 additions & 20 deletions 02_activities/assignments/assignment_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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 = \"si mei\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
"print(result)\n",
"\n",
"# Question 1\n"
]
},
{
Expand Down Expand Up @@ -88,16 +98,132 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{2}\n",
"{1: 0, 2: 1, 3: 2, 5: 2, 6: 2, 7: 2}\n",
"{2}\n",
"{1: 0, 2: 1, 3: 2, 5: 2, 6: 2, 7: 2}\n",
"{10, 12}\n",
"{1: 0, 10: 1, 2: 1, 3: 2, 12: 2}\n",
"{10, 12}\n",
"{1: 0, 2: 1, 10: 1, 12: 2, 3: 2}\n",
"set()\n",
"{10: 0, 9: 1, 7: 1, 8: 2}\n"
]
}
],
"source": [
"# 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",
"from collections import deque\n",
"\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",
" # TODO"
" seen = dict()\n",
" duplicates = set()\n",
" \n",
" value = deque([(root, 0)])\n",
" while value:\n",
" node, depth = value.popleft()\n",
" if node.val not in seen:\n",
" seen[node.val] = depth\n",
" else:\n",
" duplicates.add(node.val) \n",
" \n",
" if node.left:\n",
" value.append((node.left, depth + 1))\n",
" if node.right:\n",
" value.append((node.right, depth + 1))\n",
" \n",
" if len(duplicates) == 0:\n",
" return -1\n",
" \n",
" curr_shallowest_duplicate = None\n",
" curr_shallowest_duplicate_depth = None\n",
" for duplicate in duplicates:\n",
" depth = seen[duplicate]\n",
" if curr_shallowest_duplicate_depth is None or depth < curr_shallowest_duplicate_depth:\n",
" curr_shallowest_duplicate = duplicate\n",
" curr_shallowest_duplicate_depth = depth\n",
" \n",
" return curr_shallowest_duplicate\n",
"\n",
"\n",
"# 1\n",
"# / \\\n",
"# 2 2\n",
"# / \\ / \\\n",
"# 3 5 6 7\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",
"assert is_duplicate(root) == 2\n",
"\n",
"# 1\n",
"# / \\\n",
"# 2 2\n",
"# / \\ / \\\n",
"# 3 5 6 7\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",
"assert is_duplicate(root) == 2\n",
"\n",
"# 1\n",
"# / \\\n",
"# 10 2\n",
"# / \\ / \\\n",
"# 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",
"assert is_duplicate(root) == 10\n",
"\n",
"# 1\n",
"# / \\\n",
"# 2 10\n",
"# / \\ / \\\n",
"# 12 12 3 10\n",
"root = TreeNode(1)\n",
"root.left = TreeNode(2)\n",
"root.right = TreeNode(10)\n",
"root.left.left = TreeNode(12)\n",
"root.left.right = TreeNode(12)\n",
"root.right.left = TreeNode(3)\n",
"root.right.right = TreeNode(10)\n",
"assert is_duplicate(root) == 10\n",
"\n",
"# 10\n",
"# / \\\n",
"# 9 7\n",
"# /\n",
"# 8\n",
"root = TreeNode(10)\n",
"root.left = TreeNode(9)\n",
"root.right = TreeNode(7)\n",
"root.left.left = TreeNode(8)\n",
"assert is_duplicate(root) == -1"
]
},
{
Expand Down Expand Up @@ -227,7 +353,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# partner's problem is to move all 0's to the end of a list and maintain the orders of the non-0s"
]
},
{
Expand All @@ -244,7 +371,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# input nums = [0, -1, 0, 2, 0, 0, 3]\",\n",
"# \"Output: [-1, 2, 3, 0, 0, 0, 0]"
]
},
{
Expand All @@ -261,7 +390,22 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" insert_pos = 0\n",
"\n",
" for num in nums:\n",
" if num != 0:\n",
" nums[insert_pos] = num\n",
" insert_pos += 1\n",
" \n",
" while insert_pos < len(nums):\n",
" nums[insert_pos] = 0\n",
" insert_pos += 1\n",
"\n",
" return nums"
]
},
{
Expand All @@ -278,7 +422,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# First loop, position starts at 0, then if it is a non-zero number, replace the value at the 0 position, and then add 1 to the position \n",
"# if the next number is 0, skip it and you will not be adding 1 \n",
"# if the position number is less than the length of the input, then add 0 and add 1 position."
]
},
{
Expand All @@ -295,7 +442,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# Time: O(n), the algorithm visits each value in the input list once\n",
"# Space: O(1), the algorithm manipulates the input in-place so only needs constant additional "
]
},
{
Expand All @@ -312,7 +461,11 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# The solution works well and is quite readable. Not much to critque here. \n",
"# The current solution would loop over the list fully twice if it was all zeros, for this reason a minor improvment could be made \n",
"# so that you only have to iterate over the list once be tracking the farthest back non-zero spot and swapping with that whenever a zero is \n",
"# encountered (and then updating the farthest-back non-zero spot). "
]
},
{
Expand All @@ -338,7 +491,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# process of assignment 1 is to think about the most efficient way to do the problem. and to present in a way that is understandable for others. \n",
"# My experience reviewing is that it can be difficult to think about what could be a more efficient way to do something when you are reviewing another set of code. it is harder to think originally. \n",
"# However, my partner's code made sense in my mind and I had a good experience and learned from their code."
]
},
{
Expand Down Expand Up @@ -396,7 +552,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -410,7 +566,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down