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
164 changes: 111 additions & 53 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": 2,
"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 = \"abeer\"\n",
"result = hash_to_range(input_string)\n",
"print(result)\n"
]
Expand Down Expand Up @@ -142,118 +150,168 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 68,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Your input list was:\n",
"[62 30 71 87 0 66 55 0 1 0 0 35 0 0 0 51 88 52 0 54 0 0 95 49\n",
" 37 29 9 77 0 57 0 75 73 41 0 6 58 46 0 47 11 36 6 7]\n",
"With non-zeros moved to the end, the output list is:\n",
"[62, 30, 71, 87, 66, 55, 1, 35, 51, 88, 52, 54, 95, 49, 37, 29, 9, 77, 57, 75, 73, 41, 6, 58, 46, 47, 11, 36, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
"\n",
"The time complexity of this algorithm is O(n^2) due to the use of the insert operation.\n"
]
}
],
"source": [
"from typing import List\n",
"from numpy import random\n",
"import numpy as np\n",
"\n",
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
" # TODO\n",
" pass"
" sorted_list = [] #Create a new empty list to create sorted list with zeros at end \n",
" n=0 #Variable to iterate through nums input list \n",
" i=0 #Track number of non-zero values in the sorted_list to insert non-zeros from nums to the appropriate location \n",
" for n in range(len(nums)): #O(n), Iterate through nums input list\n",
" if nums[n]==0: #O(n), if statement \n",
" sorted_list.append(nums[n]) #O(1), If value in nums at location n is 0, append to sorted_list\n",
" n+=1 \n",
" else: \n",
" sorted_list.insert(i, nums[n]) #O(n^2), If value in nums at location n is non-zero, insert into the relevant position in sorted_list\n",
" n+=1 \n",
" i+=1 \n",
" return(sorted_list)\n",
"\n",
"#Generate a random array with zero and non-zero values of length 31 - 51, for testing. \n",
"nums = random.randint(0, 100, size=30) #Generate 30 random numbers between 0 and 100\n",
"nums = np.append(nums, [0]*random.randint(1, 20)) #Append 1 to 20 (random) 0s to nums\n",
"random.shuffle(nums) #Randomly shuffle all the values in nums i.e., spreading 0s across entire array \n",
"\n",
"#Move all zeros in nums to the end. \n",
"res = move_zeros_to_end(nums)\n",
"print(f\"Your input list was:\\n{nums}\\nWith non-zeros moved to the end, the output list is:\\n{res}\")\n",
"\n",
"print(f\"\\nThe time complexity of this algorithm is O(n^2) due to the use of the insert operation.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Part 2:\n",
"\n",
"- Paraphrase the problem in your own words\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"**Question 2.1:** Paraphrase the problem in your own words\n",
"\n",
"Move all zero values from their location in a list, `nums`, to the end of the list; keep all non-zero values in the same relative location. I.e., the list, `nums` should remain the same length with the only change being zero values are now at the end of the list."
]
},
{
"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"
"**Question 2.2**: 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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example 1: \n",
"Input: [1 0 7 8 0 9 0 7]\n",
"Output: [1, 7, 8, 9, 7, 0, 0, 0]\n",
"Example 2: \n",
"Input: [5 0 0 1 0 0 5 6]\n",
"Output: [5, 1, 5, 6, 0, 0, 0, 0]\n"
]
}
],
"source": [
"# Your answer here"
"def create_list(): \n",
" nums = random.randint(0, 10, size=5) #Generate 5 random numbers between 0 and 10\n",
" nums = np.append(nums, [0]*random.randint(1, 5)) #Append 1 to 5 (random) 0s to nums\n",
" random.shuffle(nums) #Randomly shuffle all the values in nums i.e., spreading 0s across entire array \n",
" return nums\n",
"\n",
"#For loop to create and print 2 new examples \n",
"\n",
"for i in range(2): #Create 2 example outputs \n",
" nums = create_list() #Create an intake nums list for the example, based on the loop \n",
" res = move_zeros_to_end(nums) #Call the move_zeros_to_end function to generate the output \n",
" i+=1 \n",
" print(f\"Example {i}: \\nInput: {nums}\\nOutput: {res}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2.3:** 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",
"\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": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"I accidentally coded the solution in the example code block above. I then called the function to generate my response to *Question 2.1*. For the sake of my own sanity--and so the code block for *Question 2.1* still runs--I am keeping the response code block above. I'm happy to update this response if needed and move the code over. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Question 2.4:** Explain why your solution works\n",
"\n",
"- Explain why your solution works\n"
"The solution works because it iterates through every position in the list, `num`, to check if the value is `0`. If yes, append it to the end of a new list, `sorted_list`. If the value is `non-0`, addit to the new list preceding the first `0` but after the last `non-0` value. This ensures the original `non-0` value order is retained but all the `0` values are at the end of the new list, `sorted_list`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"**Question 2.5:** Explain the problem’s time and space complexity\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The problem's Time Complexity is *O(n^2)* because of the insert operation, which requires moving every subsequent item in the list to the right by one spot. Inserting is *O(n)*, and if iterated over the entire list of length, `n`, would be `n x n = n**2` or *O(n^2)* time complexity. The if statement (*O(n)*) in the `for` loop (*O(n)*) has time complexity *O(n)* because *O(n+n)* = *O(2n)* = *O(n)*. \n",
"\n",
"- Explain the problem’s time and space complexity\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"**Time Complexity:** *O(n^2 + n)* = *O(n^2)*.\n",
"\n",
"The problem's Space Complexity is *O(n)* because the only data being stored is the original list of length `n` and the `sorted_list`, also of length `n`. Space complexity is therefore O(n).\"\n",
"\n",
"**Space Complexity:** *O(n+n)* = *O(2n)* = *O(n)*"
]
},
{
"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"
"**Question 2.6:** 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": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"An alternate approach can be used which involves creating a new list only including zero values and appending them to the original list of non-zero values after removing all the zero values. The opposite can also be done with non-zero values. \n",
"\n",
"Step-by-step: \n",
"\n",
"1. Create two empty lists: `nums` and `zeros`. \n",
"2. Assign values into the list `nums`. \n",
"3. Check if the value at index `n` in the list, `nums` is 0. (Note: if this is the first time checking a value, `n=0` )\n",
"4. If yes, append that value to list `zeros` and delete the value from `nums`. \n",
"5. If no, move onto the next value, `n+1`. \n",
"6. Repeat steps 3 - 5 for the length of `nums`. \n",
"6. Once all of `nums` has been traversed, append the list `zeros` to `nums`. \n",
"7. Print the new output list, `nums`. (Optional: clear the `zeros` list to free memory.)"
]
},
{
Expand Down Expand Up @@ -301,7 +359,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "dsi_participant",
"language": "python",
"name": "python3"
},
Expand All @@ -315,7 +373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down