Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UofT-DSI | Algorithms and Data Structures - Assignment 1 #43

Closed
wants to merge 1 commit into from
Closed
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
156 changes: 127 additions & 29 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 58,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"print((hash('your first name') % 3) + 1)"
"print((hash('Sehroz') % 3) + 1)"
]
},
{
Expand Down Expand Up @@ -72,9 +80,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (2850872121.py, line 8)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[59], line 8\u001b[0;36m\u001b[0m\n\u001b[0;31m # TODO\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
]
}
],
"source": [
"# Definition for a binary tree node.\n",
"# class TreeNode(object):\n",
Expand Down Expand Up @@ -124,7 +141,16 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block (1667981786.py, line 8)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[2], line 8\u001b[0;36m\u001b[0m\n\u001b[0;31m # TODO\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n"
]
}
],
"source": [
"# Definition for a binary tree node.\n",
"# class TreeNode(object):\n",
Expand Down Expand Up @@ -174,12 +200,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"def missing_num(nums: List) -> int:\n",
" # TODO"
"from typing import List\n",
"\n",
"def missing_num(nums: List[int]) -> int:\n",
" \n",
" if not nums:\n",
" return -1 \n",
" \n",
" n = max(nums) ## Largest num in list\n",
"\n",
" full_range = set(range(n + 1)) ## Creates a set for all ints between 0 & n\n",
"\n",
" nums_set = set(nums) ## Converts list into set\n",
"\n",
" missing = list((full_range - nums_set)) ## Removes ints from nums_set from full_range leaving the missing numbers & converts into a list\n",
" \n",
" return missing\n",
"\n",
"\n"
]
},
{
Expand All @@ -194,11 +236,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"# Given a list of numbers [0, n] & we need to find the missing numbers from the full range (0 to n).\n",
"# The result should display the missing numbers in list, if there are no missing numbers then it should return -1."
]
},
{
Expand All @@ -210,11 +255,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"nums = [0, 1, 3, 6, 7]\n",
"\n",
"nums = [0, 3, 8, 20]"
]
},
{
Expand All @@ -227,11 +275,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 63,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n"
]
}
],
"source": [
"# Your answer here"
"# Your answer here\n",
"def missing_num(nums: List[int]) -> int:\n",
"\n",
" if not nums:\n",
" return -1 \n",
" \n",
" n = max(nums) ## Largest num in list\n",
"\n",
" full_range = set(range(n + 1)) ## Creates a set for all ints between 0 & n\n",
"\n",
" nums_set = set(nums) ## Converts list into set\n",
"\n",
" missing = list((full_range - nums_set)) ## Removes ints from nums_set from full_range leaving the missing numbers & converts into a list\n",
" \n",
" return missing\n",
"\n",
"print(missing_num(nums))"
]
},
{
Expand All @@ -244,11 +316,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"# My solution checks which #'s should be there (from 0 to the largest number) and compares this with the numbers we actually have in the list. \n",
"# It then tells us which ones are missing in order. If none are missing, it just says -1. "
]
},
{
Expand All @@ -261,28 +336,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"## Time complexty = O(n + k)\n",
"# A) Finding the Maximum (max(nums)) = O(n)\n",
"# B) Creating the Full Range (set(range(n + 1)))= O(n)\n",
"# C) Converting the Input List to a Set (set(nums))= O(n)\n",
"# D) Set Subtraction (full_range - nums_set) = O(n)\n",
"# E) Converting the Result to a List = O(k)\n",
"\n",
"## Space Complexity = O(n + k)\n",
"# A) Full Range Set (set(range(n + 1))) = O(n)\n",
"# B) Input List Set (set(nums)) = O(n)\n",
"# C) Missing Numbers List = O(k)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Explain the thinking to an alternative solution (no coding required, but a classmate reading this should be able to code it up based off your text)\n"
"- Explain the thinking to an alternative solution (no coding required, but a classmate reading this should be able to code it up based off your text)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"\n",
"# 1. Find the largest number in the list by identifying the max value for the range `[0, n]`.\n",
"\n",
"# 2. Create a boolean array of size n + 1 with all values set to False.\n",
"\n",
"# 3. Mark numbers as found by setting True for indices matching to the numbers in the list.\n",
"\n",
"# 4. Identify missing numbers by collecting the indices that remain marked as False.\n",
"\n",
"# 5. Handle edge cases like an empty list or no missing numbers to ensure the output is correct.\n"
]
},
{
Expand Down Expand Up @@ -319,18 +417,18 @@
" * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n",
"\n",
"Checklist:\n",
"- [ ] Create a branch called `assignment-1`.\n",
"- [ ] Ensure that the repository is public.\n",
"- [ ] Review [the PR description guidelines](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md#guidelines-for-pull-request-descriptions) and adhere to them.\n",
"- [ ] Verify that the link is accessible in a private browser window.\n",
"- [x] Create a branch called `assignment-1`.\n",
"- [x] Ensure that the repository is public.\n",
"- [x] Review [the PR description guidelines](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md#guidelines-for-pull-request-descriptions) and adhere to them.\n",
"- [x] Verify that the link is accessible in a private browser window.\n",
"\n",
"If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#cohort-3-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "sehroz",
"language": "python",
"name": "python3"
},
Expand All @@ -344,7 +442,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.9.20"
}
},
"nbformat": 4,
Expand Down
Loading