Skip to content

Commit 5ebf531

Browse files
author
azharhasan
committed
adding revised answers to assignment 1 problems
1 parent 4dba3ed commit 5ebf531

File tree

1 file changed

+94
-18
lines changed

1 file changed

+94
-18
lines changed

02_activities/assignments/assignment_1.ipynb

Lines changed: 94 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,25 @@
2525
},
2626
{
2727
"cell_type": "code",
28-
"execution_count": null,
28+
"execution_count": 1,
2929
"metadata": {},
30-
"outputs": [],
30+
"outputs": [
31+
{
32+
"name": "stdout",
33+
"output_type": "stream",
34+
"text": [
35+
"3\n"
36+
]
37+
}
38+
],
3139
"source": [
3240
"import hashlib\n",
3341
"\n",
3442
"def hash_to_range(input_string: str) -> int:\n",
3543
" hash_object = hashlib.sha256(input_string.encode())\n",
3644
" hash_int = int(hash_object.hexdigest(), 16)\n",
3745
" return (hash_int % 3) + 1\n",
38-
"input_string = \"your_first_name_here\"\n",
46+
"input_string = \"azhar\"\n",
3947
"result = hash_to_range(input_string)\n",
4048
"print(result)\n"
4149
]
@@ -142,15 +150,51 @@
142150
},
143151
{
144152
"cell_type": "code",
145-
"execution_count": null,
153+
"execution_count": 75,
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"nums=[0,1,0,3,12]"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 70,
146163
"metadata": {},
147164
"outputs": [],
148165
"source": [
149166
"from typing import List\n",
150167
"\n",
151168
"def move_zeros_to_end(nums: List[int]) -> List[int]:\n",
152-
" # TODO\n",
153-
" pass"
169+
" results=[0] * len(nums)\n",
170+
" i=0\n",
171+
" for num in nums:\n",
172+
" if num!=0:\n",
173+
" results[i]=num\n",
174+
" i=i+1\n",
175+
" else:\n",
176+
" pass\n",
177+
" return results"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": 71,
183+
"metadata": {},
184+
"outputs": [
185+
{
186+
"data": {
187+
"text/plain": [
188+
"[1, 3, 12, 0, 0]"
189+
]
190+
},
191+
"execution_count": 71,
192+
"metadata": {},
193+
"output_type": "execute_result"
194+
}
195+
],
196+
"source": [
197+
"move_zeros_to_end(nums)"
154198
]
155199
},
156200
{
@@ -169,7 +213,7 @@
169213
"metadata": {},
170214
"outputs": [],
171215
"source": [
172-
"# Your answer here"
216+
"print(\"The problem wants to sort a list of numbers in a fashion that zeroes show up at the end of list while the order of the non zero values is preserved\") "
173217
]
174218
},
175219
{
@@ -181,11 +225,26 @@
181225
},
182226
{
183227
"cell_type": "code",
184-
"execution_count": null,
228+
"execution_count": 38,
185229
"metadata": {},
186-
"outputs": [],
230+
"outputs": [
231+
{
232+
"name": "stdout",
233+
"output_type": "stream",
234+
"text": [
235+
"[-3, 1, 100, 5, 0, 0, 0]\n",
236+
"[1, 100, 5, 32, 63, 0, 0, 0, 0]\n"
237+
]
238+
}
239+
],
187240
"source": [
188-
"# Your answer here"
241+
"Input_1 = [-3,0,1,100,0,0,5]\n",
242+
"output_1=move_zeros_to_end(Input_1)\n",
243+
"Input_2 = [0,0,1,100,0,0,5,32,63]\n",
244+
"output_2=move_zeros_to_end(Input_2)\n",
245+
"\n",
246+
"print(output_1)\n",
247+
"print(output_2)"
189248
]
190249
},
191250
{
@@ -202,7 +261,10 @@
202261
"metadata": {},
203262
"outputs": [],
204263
"source": [
205-
"# Your answer here"
264+
"# Your answer here\n",
265+
"print(\"Since the order of elements in the object is important. List is the most appropriate abstract data type for this problem. The solution provided above\" \\\n",
266+
"\"has a time complexity of O(n) because we are only doing assignment operations, an O(1) complexity task, n times. Space complexity requires 2 lists and therefore\" \\\n",
267+
"\"the space complexity is O(1)\")"
206268
]
207269
},
208270
{
@@ -219,7 +281,8 @@
219281
"metadata": {},
220282
"outputs": [],
221283
"source": [
222-
"# Your answer here"
284+
"# Your answer here\n",
285+
"print(\" As explained earlier, solutions with O(n) and O(nlogn) are the best solutions\")"
223286
]
224287
},
225288
{
@@ -236,7 +299,9 @@
236299
"metadata": {},
237300
"outputs": [],
238301
"source": [
239-
"# Your answer here"
302+
"# Your answer here\n",
303+
"print(\"The problem needs two iterables, 1 that knows the value of current item in the list and one that keeps it's position in the new list. \" \\\n",
304+
"\"Since there's only 1 variable, time complexity in O(n) and space complexity of O(1)\")"
240305
]
241306
},
242307
{
@@ -249,11 +314,22 @@
249314
},
250315
{
251316
"cell_type": "code",
252-
"execution_count": null,
317+
"execution_count": 86,
253318
"metadata": {},
254-
"outputs": [],
319+
"outputs": [
320+
{
321+
"name": "stdout",
322+
"output_type": "stream",
323+
"text": [
324+
"An alternative solution can potentially use one of the recursion techniques. Say i use two iterable, one keeps the position in nums that's read and the other keep position in the results array that needs to updated based on the valueThe base function would essentially increment the read position by 1 and write position by 1 if the read positions value is non-zero, else it will only increment the read position\n"
325+
]
326+
}
327+
],
255328
"source": [
256-
"# Your answer here"
329+
"# Your answer here\n",
330+
"print(\"An alternative solution can potentially use one of the recursion techniques. Say i use two iterable, one keeps the position in nums that's read and the other keep position in the results array that needs to updated based on the value\" \\\n",
331+
"\"The base function would essentially increment the read position by 1 and write position by 1 if the read positions value is non-zero, \" \\\n",
332+
"\"else it will only increment the read position\")"
257333
]
258334
},
259335
{
@@ -301,7 +377,7 @@
301377
],
302378
"metadata": {
303379
"kernelspec": {
304-
"display_name": "Python 3 (ipykernel)",
380+
"display_name": "dsi_participant",
305381
"language": "python",
306382
"name": "python3"
307383
},
@@ -315,7 +391,7 @@
315391
"name": "python",
316392
"nbconvert_exporter": "python",
317393
"pygments_lexer": "ipython3",
318-
"version": "3.11.5"
394+
"version": "3.9.15"
319395
}
320396
},
321397
"nbformat": 4,

0 commit comments

Comments
 (0)