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
399 changes: 399 additions & 0 deletions 02_activities/assignments/assignment_1.2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,399 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coding Problems\n",
"\n",
"## Objective\n",
"\n",
"This assignment aims to demonstrate how to study a data structures or algorithms question in depth to prepare for an industry coding interview. Leetcode is a popular coding practice site that many use to practice for technical interviews. Like behavioral interviews, it's important to practice and keep your skills sharp.\n",
"\n",
"## Group Size\n",
"\n",
"Please complete this individually.\n",
"\n",
"## Parts:\n",
"- Part 1: Figure out the problem you have been assigned, and understand the problem\n",
"- Part 2: Answer the questions about your assigned problem (including solving it)\n",
"\n",
"## Part 1:\n",
"\n",
"_*You will be assigned one of three problems based of your first name. Enter your first name, in all lower case, execute the code below, and that will tell you your assigned problem. Include the output as part of your submission (do not clear the output). The problems are based-off problems from Leetcode.*_\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"import hashlib\n",
"\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 = \"Bakary\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question One: First Duplicate in List\n",
"**Description** \n",
"Given a list of integers, return the **first value that appears more than once**. If there are multiple duplicates, return the one that appears **first** in the list. If no duplicate exists, return `-1`.\n",
"\n",
"**Examples**\n",
"```python\n",
"Input: nums = [3, 1, 4, 2, 5, 1, 6]\n",
"Output: 1\n",
"```\n",
"```python\n",
"Input: nums = [7, 8, 9, 10]\n",
"Output: -1\n",
"```\n",
"```python\n",
"Input: nums = [4, 5, 6, 4, 6]\n",
"Output: 4\n",
"```\n",
"\n",
"**Question 1 Starter Code**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"git status\n",
"\n",
"\n",
"def first_duplicate(nums: List[int]) -> int:\n",
" # TODO\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question Two: Valid Bracket Sequence\n",
"**Description** \n",
"Given a string containing only the characters `'('`, `')'`, `'{'`, `'}'`, `'['`, and `']'`, determine if the input string is a **valid bracket sequence**. \n",
"A string is valid if:\n",
"- Open brackets are closed by the same type of brackets, and\n",
"- Open brackets are closed in the correct order.\n",
"\n",
"**Examples**\n",
"```python\n",
"Input: s = \"([]{})\"\n",
"Output: True\n",
"```\n",
"```python\n",
"Input: s = \"([)]\"\n",
"Output: False\n",
"```\n",
"```python\n",
"Input: s = \"()[]{}\"\n",
"Output: True\n",
"```\n",
"```python\n",
"Input: s = \"[{]}\"\n",
"Output: False\n",
"```\n",
"\n",
"**Question 2 Starter Code**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_valid_brackets(s: str) -> bool:\n",
" # TODO\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Question Three: Move All Zeros to End\n",
"**Description** \n",
"Given a list of integers, move all zeros to the end while maintaining the relative order of the non-zero elements. \n",
"\n",
"**Examples**\n",
"```python\n",
"Input: nums = [0, 1, 0, 3, 12]\n",
"Output: [1, 3, 12, 0, 0]\n",
"```\n",
"```python\n",
"Input: nums = [4, 0, 5, 0, 0, 6]\n",
"Output: [4, 5, 6, 0, 0, 0]\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" \n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Part 2:\n",
"\n",
"- Paraphrase the problem in your own words\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Your answer here 1\n",
"\n",
"Given an array of integers that may include zeros, the task is to reorder the array \n",
"such that all zeros appear at the end, without altering the relative order of the non-zero elements\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- In this .ipynb file, there are examples that illustrate how the code should work (the examples provided above). Create 2 new examples for the question you have been assigned, that demonstrate you understand the problem. For question 1 and 2, you don't need to create the tree demonstration, just the input and output.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Your answer here 2\n",
"\n",
"input: [4, 2, 0, 1, 0, 3, 0]\n",
"output: [4, 2, 1, 3, 0, 0,0]\n",
"\n",
"\n",
"input: [0, 1, 0, 2, 3]\n",
"output: [1, 2, 3, 0, 0]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Code the solution to your assigned problem in Python (code chunk). Note: each problem can be solved more simply if you use an abstract data type that is suitable for that problem. Using that try to find the best time and space complexity solution!\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here 3\n",
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" insert_pos = 0 # Tracks where the next non-zero should go\n",
"\n",
" # Move non-zero elements forward\n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" nums[insert_pos] = nums[i]\n",
" insert_pos += 1\n",
"\n",
" # Fill remaining positions with zeros\n",
" for i in range(insert_pos, len(nums)):\n",
" nums[i] = 0\n",
"\n",
" return nums\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Explain why your solution works\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4, 2, 1, 3, 0, 0, 0]\n",
"[1, 2, 3, 0, 0]\n"
]
}
],
"source": [
"# Your answer here 4\n",
"print(move_zeros_to_end([4, 2, 0, 1, 0, 3, 0])) # [4, 2, 1, 3, 0, 0, 0]\n",
"print(move_zeros_to_end([0, 1, 0, 2, 3])) # [1, 2, 3, 0, 0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"1- Step One: Move Non-Zeros Forward: Go through the list and put each non-zero number at the fron, and keep the original order of the non-zero numbers.\n",
"\n",
"2- Step Two: Add Zeros at the End. After all non-zero numbers are placed, fill the rest of the list with zeros. This makes sure all zeros are at the end without changing the order of the other numbers.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"- Explain the problem’s time and space complexity\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Your answer here 5\n",
"Time Complexity: O(n) — Linear Time\n",
"First Step, the fuction moves Non-Zeros and checks each item in the list. Thant takes O(n) times, with n items in the list once. This takes O(n) time. Second, the fuction fill the rest of the list with zeros. \n",
"In the worst case (if all items were zeros), this also takes O(n) time. Then the total O(n) + O(n) = O(2n) we simplify O(n), because the growth rate depends on n not the constant 2.\n",
"\n",
"In term of space :\n",
"The function uses very little memory — just a few variables. It changes the list directly without making a new one. So, no matter how big the list is, memory use stays the same: O(1)."
]
},
{
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Your answer here 6\n",
"\n",
"Instead of changing the original list, we make a new one:\n",
"\n",
"1- Add all the non-zero numbers to the new list.\n",
"\n",
"2- Count how many zeros were in the original list.\n",
"\n",
"3-Add that many zeros to the end.\n",
"\n",
"4- Return the new list."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Evaluation Criteria\n",
"\n",
"- Problem is accurately stated\n",
"\n",
"- Two examples are correct and easily understandable\n",
"\n",
"- Correctness, time, and space complexity of the coding solution\n",
"\n",
"- Clarity in explaining why the solution works, its time and space complexity\n",
"\n",
"- Clarity in the proposal to the alternative solution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Submission Information\n",
"\n",
"🚨 **Please review our [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md)** 🚨 for detailed instructions on how to format, branch, and submit your work. Following these guidelines is crucial for your submissions to be evaluated correctly.\n",
"\n",
"### Submission Parameters:\n",
"* Submission Due Date: `HH:MM AM/PM - DD/MM/YYYY`\n",
"* The branch name for your repo should be: `assignment-1`\n",
"* What to submit for this assignment:\n",
" * This Jupyter Notebook (assignment_1.ipynb) should be populated and should be the only change in your pull request.\n",
"* What the pull request link should look like for this assignment: `https://github.com/<your_github_username>/algorithms_and_data_structures/pull/<pr_id>`\n",
" * 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",
"\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": "dsi_participant",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
}
},
"nbformat": 4,
"nbformat_minor": 4
}