Skip to content
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
112 changes: 82 additions & 30 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 40,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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 = \"jonah\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -67,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -112,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -142,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -164,12 +172,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"Given a list of integers, use the binary classfication $A = \\mathbb{Z}\\setminus{}\\{0\\}, B = \\{0\\}$ to sort the list in ascending order by the condition $a < b $ for $a\\in{}A, b\\in{}B$, where the elements in $A$ are sorted in a stable manner."
]
},
{
Expand All @@ -180,12 +186,17 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"```python\n",
"Input: nums = [8, -1 , 0, 0, 5, 6, 7, 0]\n",
"Output: [8, -1 , 5, 6, 7, 0, 0, 0]\n",
"```\n",
"```python\n",
"Input: nums = [4, 42, 9, 20, 59, 0, 0, 0, 88]\n",
"Output: [4, 42, 9, 20, 59, 88, 0, 0, 0]\n",
"```"
]
},
{
Expand All @@ -198,11 +209,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"from collections import deque\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" #queue to record indices of zeros in ascending order\n",
" zero_inds = deque()\n",
" #forward loop through list\n",
" for i in range(len(nums)):\n",
" if nums[i] == 0:\n",
" #record index of zero\n",
" zero_inds.append(i)\n",
" elif zero_inds:\n",
" #non-zero element encountered after at least one zero element\n",
" #swap non-zero element with first zero\n",
" first_zero_ind = zero_inds.popleft()\n",
" nums[i], nums[first_zero_ind] = nums[first_zero_ind], nums[i]\n",
" #a zero now exists at index i. append i to the queue\n",
" zero_inds.append(i)\n",
" return nums\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"assert move_zeros_to_end([0, 1, 0, 3, 12]) == [1, 3, 12, 0, 0]\n",
"assert move_zeros_to_end([4, 0, 5, 0, 0, 6]) == [4, 5, 6, 0, 0, 0]\n",
"assert move_zeros_to_end([8, -1 , 0, 0, 5, 6, 7, 0]) == [8, -1 , 5, 6, 7, 0, 0, 0]\n",
"assert move_zeros_to_end([4, 42, 9, 20, 59, 0, 0, 0, 88]) == [4, 42, 9, 20, 59, 88, 0, 0, 0]"
]
},
{
Expand All @@ -214,12 +257,18 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"My solution forward loops through the list. When it encounters a zero, it adds the corresponding index to a queue, establishing a record of zero indices in ascending order. When a non-zero is encountered after at least one zero, it is swapped with the first zero, which is identified by left popping its index off the queue. Now, a zero exists at the highest index yet encountered (i.e., it is the last known zero), so it is appended to the queue.\n",
"\n",
"Correctness can be shown by induction on the length of the list, $n$.\n",
"\n",
"Base case ($n=0$):\n",
"An empty list trivially satisfies the solution.\n",
"\n",
"Inductive case:\n",
"Assume that the first $n-1$ elements satisfy the condition. Then, the list contains $i$ non-zero elements, followed by $n-1-i$ zeros. If the $n$'th element is a zero, then the condition is already satisfied. If the $n$'th element is not zero, then it is swapped with the first zero, establishing a sequence of $i+1$ non-zeros followed by $n-1-i$ zeros."
]
},
{
Expand All @@ -231,12 +280,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The solution has $O(n)$ time complexity and $O(n)$ space complexity.\n",
"\n",
"Time complexity: since Python's `deque` implementation maintains pointers to both the beginning and end of the list, appending and left popping from the queue are both $O(1)$ time operations. Therefore, the solution does $O(1)$ work for each of the $n$ elements in the list.\n",
"\n",
"Space complexity: the space complexity can be no less than $O(n)$, since the list consumes $O(n)$ space. Conversely, the `deque` of zero indices can have at most $n$ elements, an upper bound that is achieved when the input is a list of only zeros. "
]
},
{
Expand All @@ -248,12 +299,10 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"An alternative solution would be to use a bubble sort-like approach to achieve an $O(n^2)$ time complexity and an $O(n)$ space complexity. More explcitly, one could use two nested loops. The outer loop traverses each position in the list. The inner loop traverses from the beginning to the end, reduced by the number of iterations that the outer loop has completed + 1. If it encounters a zero followed immediately by a non-zero, it swaps those two elements. In that way, it \"bubbles\" zeros to the end. Each iteration of the outer loop results in a single additional zero positioned at the end, up to the number of zeros in the list. This solution has $O(n)$ space complexity since an $n$ element input list consumes $O(n)$ space. Although this alternative does not reduce the space complexity compared to my solution, it does obviate the need for the additional data structure (the `deque`)."
]
},
{
Expand Down Expand Up @@ -300,8 +349,11 @@
}
],
"metadata": {
"interpreter": {
"hash": "7ccc5368cda896b2ab91e5e6fe72ec210001cb3dc1ca1ba63746389bd9a7a2c9"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3.11.14 64-bit (system)",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +367,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down