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
99 changes: 73 additions & 26 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@
},
{
"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",
"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 = \"mike\"\n",
"result = hash_to_range(input_string)\n",
"print(f\"Your assigned problem is: {result}\")"
]
Expand Down Expand Up @@ -128,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -162,15 +170,47 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" pass"
" current_modify_index = 0\n",
" for i in range(len(nums)):\n",
" if nums[i] != 0:\n",
" nums[current_modify_index] = nums[i]\n",
" current_modify_index += 1\n",
"\n",
" for i in range(current_modify_index, len(nums)):\n",
" nums[i] = 0\n",
"\n",
" return nums"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 12, 0, 0]\n",
"[4, 5, 6, 0, 0, 0]\n",
"[3, 2, 4, 0, 0, 0, 0]\n",
"[1, 3, 4, 3, 4, 0, 0]\n"
]
}
],
"source": [
"print(move_zeros_to_end([0,1,0,3,12]))\n",
"print(move_zeros_to_end([4,0,5,0,0,6]))\n",
"\n",
"print(move_zeros_to_end([0,0,0,3,0,2,4]))\n",
"print(move_zeros_to_end([1,3,4,3,4,0,0]))\n"
]
},
{
Expand All @@ -185,11 +225,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"#The problem is asking to move all the zeros to the end of the list while keeping the order of the non zero values intact."
]
},
{
Expand All @@ -201,11 +241,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Your answer here\n",
"# Example 3:\n",
"# Input: nums = [0,0,0,3,0,2,4];\n",
"# Output: [3,2,4,0,0,0,0]\n",
"\n",
"# Example 4:\n",
"# Input: nums = [1,3,4,3,4,0,0]\n",
"# Output: [1,3,4,3,4,0,0]\n",
"\n"
]
},
{
Expand All @@ -215,15 +263,6 @@
"3. Based on the provided starter code, code the solution to your assigned problem here in Python. Note: each problem can be solved more simply if you use an abstract data type that is suitable for that problem. Try to find the solution with best time and space complexity."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -238,7 +277,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"#I set a current_modify_index variable to keep track of which index in the current nums list I can overwrite. I want to \n",
"# overwrite the existing nums list. I go through a for loop and check if the current index is not 0, and then I assign the \n",
"# current value of index i to current modify index. I then increment the index by 1 as my next candidate that can be modified\n",
"# Once I'm done finding all non zero numbers and moved them to the front of the list, I then loop through my current modify index \n",
"# to end of nums array to zero out the remaining indices. Non zero numbers keep their order because you read from left to right\n",
"# and write them into consecutive indices.\n",
"#"
]
},
{
Expand All @@ -255,7 +300,8 @@
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# The time complexity is O(n) as we have to go iterate the list n times in the first loop, and the second loop runs at most n times, so it's O(n). The space complexity is O(1) as I'm modifying the \n",
"# existing nums list in place."
]
},
{
Expand All @@ -268,11 +314,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Another way to do this is to create a new list and add the non zero numbers to that list while counting how many zeros there are. \n",
"# Once the non zero numbers have been added, you then append the zeros you counted to the end of the list and return the new list"
]
},
{
Expand Down Expand Up @@ -321,7 +368,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "algos-env (3.11.14)",
"language": "python",
"name": "python3"
},
Expand All @@ -335,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down