Skip to content

Commit 5721417

Browse files
authored
Merge pull request #2 from k2jac9/assignment_2
Assignment 2
2 parents 05b2e3b + ae08fc9 commit 5721417

File tree

3 files changed

+638
-72
lines changed

3 files changed

+638
-72
lines changed

02_activities/assignments/assignment_1.ipynb

+221-41
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@
2121
},
2222
{
2323
"cell_type": "code",
24-
"execution_count": null,
24+
"execution_count": 1,
2525
"metadata": {},
26-
"outputs": [],
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"3\n"
32+
]
33+
}
34+
],
2735
"source": [
2836
"import hashlib\n",
2937
"\n",
3038
"def hash_to_range(input_string: str) -> int:\n",
3139
" hash_object = hashlib.sha256(input_string.encode())\n",
3240
" hash_int = int(hash_object.hexdigest(), 16)\n",
3341
" return (hash_int % 3) + 1\n",
34-
"input_string = \"your_first_name_here\"\n",
42+
"input_string = \"alejandro castellanos\"\n",
3543
"result = hash_to_range(input_string)\n",
36-
"print(result)\n"
44+
"print(result) # Output: 3\n"
3745
]
3846
},
3947
{
@@ -80,7 +88,7 @@
8088
},
8189
{
8290
"cell_type": "code",
83-
"execution_count": null,
91+
"execution_count": 2,
8492
"metadata": {},
8593
"outputs": [],
8694
"source": [
@@ -90,8 +98,7 @@
9098
"# self.val = val\n",
9199
"# self.left = left\n",
92100
"# self.right = right\n",
93-
"def is_duplicate(root: TreeNode) -> int:\n",
94-
" # TODO"
101+
"# def is_duplicate(root: TreeNode) -> int:\n"
95102
]
96103
},
97104
{
@@ -130,7 +137,7 @@
130137
},
131138
{
132139
"cell_type": "code",
133-
"execution_count": null,
140+
"execution_count": 3,
134141
"metadata": {},
135142
"outputs": [],
136143
"source": [
@@ -140,8 +147,7 @@
140147
"# self.val = val\n",
141148
"# self.left = left\n",
142149
"# self.right = right\n",
143-
"def bt_path(root: TreeNode) -> List[List[int]]:\n",
144-
" # TODO"
150+
"# def bt_path(root: TreeNode) -> List[List[int]]:\n"
145151
]
146152
},
147153
{
@@ -180,49 +186,152 @@
180186
"#### Starter Code for Question 3\n"
181187
]
182188
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {},
192+
"source": [
193+
"### Solving for missing numbers within the valid range"
194+
]
195+
},
183196
{
184197
"cell_type": "code",
185-
"execution_count": null,
198+
"execution_count": 4,
186199
"metadata": {},
187200
"outputs": [],
188201
"source": [
189-
"def missing_num(nums: List) -> int:\n",
190-
" # TODO"
202+
"def missing_num(nums):\n",
203+
" # Find the largest number in the list\n",
204+
" n = max(nums) # The largest number should be the upper bound\n",
205+
" \n",
206+
" # Create a set of the numbers in the list (automatically removes duplicates)\n",
207+
" num_set = set(nums)\n",
208+
" \n",
209+
" # Generate the list of missing numbers\n",
210+
" missing = [i for i in range(n + 1) if i not in num_set]\n",
211+
" \n",
212+
" # If no numbers are missing, return -1\n",
213+
" if not missing:\n",
214+
" return -1\n",
215+
" \n",
216+
" return missing\n",
217+
"\n"
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": 5,
223+
"metadata": {},
224+
"outputs": [
225+
{
226+
"name": "stdout",
227+
"output_type": "stream",
228+
"text": [
229+
"-1\n",
230+
"-1\n",
231+
"[1]\n",
232+
"[2, 3, 4]\n",
233+
"[4, 9]\n"
234+
]
235+
}
236+
],
237+
"source": [
238+
"print(missing_num([0])) # Output: -1\n",
239+
"print(missing_num([0, 1])) # Output: -1\n",
240+
"print(missing_num([0, 2])) # Output: [1]\n",
241+
"print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n",
242+
"print(missing_num([6, 8, 2, 3, 5, 7, 0, 1, 10])) # Output: [4, 9]"
191243
]
192244
},
193245
{
194246
"cell_type": "markdown",
195247
"metadata": {},
196248
"source": [
197-
"\n",
198-
"## Part 2:\n",
199-
"\n",
200-
"- Paraphrase the problem in your own words\n"
249+
"### Importing List from typing"
250+
]
251+
},
252+
{
253+
"cell_type": "markdown",
254+
"metadata": {},
255+
"source": [
256+
"### Solving for missing numbers within the valid range"
201257
]
202258
},
203259
{
204260
"cell_type": "code",
205-
"execution_count": null,
261+
"execution_count": 6,
206262
"metadata": {},
207263
"outputs": [],
208264
"source": [
209-
"# Your answer here"
265+
"from typing import List # Import List from typing module\n",
266+
"\n",
267+
"def missing_num(nums: List[int]) -> int:\n",
268+
" # Find the largest number in the list\n",
269+
" n = max(nums) # The largest number should be the upper bound\n",
270+
" \n",
271+
" # Create a set of the numbers in the list (automatically removes duplicates)\n",
272+
" num_set = set(nums)\n",
273+
" \n",
274+
" # Generate the list of missing numbers\n",
275+
" missing = [i for i in range(n + 1) if i not in num_set]\n",
276+
" \n",
277+
" # If no numbers are missing, return -1\n",
278+
" if not missing:\n",
279+
" return -1\n",
280+
" \n",
281+
" return missing\n"
282+
]
283+
},
284+
{
285+
"cell_type": "code",
286+
"execution_count": 7,
287+
"metadata": {},
288+
"outputs": [
289+
{
290+
"name": "stdout",
291+
"output_type": "stream",
292+
"text": [
293+
"-1\n",
294+
"-1\n",
295+
"[1]\n",
296+
"[2, 3, 4]\n",
297+
"[4, 9]\n"
298+
]
299+
}
300+
],
301+
"source": [
302+
"print(missing_num([0])) # Output: -1\n",
303+
"print(missing_num([0, 1])) # Output: -1\n",
304+
"print(missing_num([0, 2])) # Output: [1]\n",
305+
"print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n",
306+
"print(missing_num([6, 8, 2, 3, 5, 7, 0, 1, 10])) # Output: [4, 9]\n"
210307
]
211308
},
212309
{
213310
"cell_type": "markdown",
214311
"metadata": {},
215312
"source": [
216-
"- 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"
313+
"\n",
314+
"## Part 2:\n",
315+
"\n",
316+
"- Paraphrase the problem in your own words\n"
217317
]
218318
},
219319
{
220-
"cell_type": "code",
221-
"execution_count": null,
320+
"cell_type": "markdown",
222321
"metadata": {},
223-
"outputs": [],
224322
"source": [
225-
"# Your answer here"
323+
"We have a list of numbers that should cover all integers from 0 to n, but some numbers may be missing or repeated. The task is to find which numbers are missing from the list.\n",
324+
"\n",
325+
"We need to identify which numbers are missing in the range from 0 to the maximum number in the list. If no numbers are missing, we return -1.\n",
326+
"\n",
327+
"This problem is about finding which numbers are missing from a list that should contain all integers from 0 to the maximum number. By using a set to remove duplicates and a list comprehension to find missing numbers, we can solve this efficiently.\n"
328+
]
329+
},
330+
{
331+
"cell_type": "markdown",
332+
"metadata": {},
333+
"source": [
334+
"- 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"
226335
]
227336
},
228337
{
@@ -235,11 +344,52 @@
235344
},
236345
{
237346
"cell_type": "code",
238-
"execution_count": null,
347+
"execution_count": 8,
239348
"metadata": {},
240349
"outputs": [],
241350
"source": [
242-
"# Your answer here"
351+
"from typing import List\n",
352+
"\n",
353+
"def missing_num(nums: List[int]) -> int:\n",
354+
" # Step 1: Find the maximum number in the list\n",
355+
" n = max(nums)\n",
356+
" \n",
357+
" # Step 2: Create a set of all numbers from 0 to n\n",
358+
" full_set = set(range(n + 1))\n",
359+
" \n",
360+
" # Step 3: Create a set from the input list\n",
361+
" num_set = set(nums)\n",
362+
" \n",
363+
" # Step 4: Find the missing numbers\n",
364+
" missing = list(full_set - num_set)\n",
365+
" \n",
366+
" # Step 5: Return the missing numbers or -1 if there are none\n",
367+
" return missing if missing else -1\n"
368+
]
369+
},
370+
{
371+
"cell_type": "code",
372+
"execution_count": 9,
373+
"metadata": {},
374+
"outputs": [
375+
{
376+
"name": "stdout",
377+
"output_type": "stream",
378+
"text": [
379+
"[2, 3, 4]\n",
380+
"[1]\n",
381+
"[1, 3, 6]\n",
382+
"-1\n",
383+
"-1\n"
384+
]
385+
}
386+
],
387+
"source": [
388+
"print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n",
389+
"print(missing_num([0, 2, 2])) # Output: [1]\n",
390+
"print(missing_num([8, 2, 0, 5, 7, 7, 4])) # Output: [1, 3, 6]\n",
391+
"print(missing_num([0, 1])) # Output: -1\n",
392+
"print(missing_num([0])) # Output: -1"
243393
]
244394
},
245395
{
@@ -251,12 +401,18 @@
251401
]
252402
},
253403
{
254-
"cell_type": "code",
255-
"execution_count": null,
404+
"cell_type": "markdown",
256405
"metadata": {},
257-
"outputs": [],
258406
"source": [
259-
"# Your answer here"
407+
"Explanation:\n",
408+
"\n",
409+
"Full Set (full_set): This set contains all the numbers from 0 to the maximum number in the list (n).\n",
410+
"\n",
411+
"Input Set (num_set): We create a set from the input list to remove duplicates.\n",
412+
"\n",
413+
"Missing Numbers: By subtracting num_set from full_set, we get the missing numbers. \n",
414+
"\n",
415+
"If there are no missing numbers, we return -1."
260416
]
261417
},
262418
{
@@ -268,12 +424,14 @@
268424
]
269425
},
270426
{
271-
"cell_type": "code",
272-
"execution_count": null,
427+
"cell_type": "markdown",
273428
"metadata": {},
274-
"outputs": [],
275429
"source": [
276-
"# Your answer here"
430+
"Time and Space Complexity:\n",
431+
"\n",
432+
"Time Complexity: O(n), where n is the length of the input list. We iterate over the list twice (once to create the set and once to subtract the sets).\n",
433+
"\n",
434+
"Space Complexity: O(n), for the sets (full_set and num_set) used to store the numbers."
277435
]
278436
},
279437
{
@@ -285,12 +443,34 @@
285443
]
286444
},
287445
{
288-
"cell_type": "code",
289-
"execution_count": null,
446+
"cell_type": "markdown",
290447
"metadata": {},
291-
"outputs": [],
292448
"source": [
293-
"# Your answer here"
449+
"Alternative Approach: Using a Boolean Array (Frequency Array) to Track Presence\n",
450+
"\n",
451+
"Determine the Range:\n",
452+
"\n",
453+
"First, find the maximum value in the input list. This value (let's call it max_value) represents the highest number we expect in the complete range from 0 to max_value.\n",
454+
"Initialize a Boolean Array:\n",
455+
"\n",
456+
"Create a boolean array (or list) called seen with a length of max_value + 1.\n",
457+
"Initialize every element to False. This array will track which numbers have been encountered.\n",
458+
"Mark the Numbers Present:\n",
459+
"\n",
460+
"Iterate through the input list.\n",
461+
"For each number num in the list, set seen[num] to True (this marks that the number num is present in the list).\n",
462+
"Identify Missing Numbers:\n",
463+
"\n",
464+
"Iterate through the indices of the seen array (from 0 to max_value).\n",
465+
"Collect the indices where the value remains False because these indices represent the numbers that were missing from the input list.\n",
466+
"Return the Result:\n",
467+
"\n",
468+
"If you find any missing numbers, return the list of these numbers.\n",
469+
"If no missing numbers are found (i.e., all indices are marked True), return -1.\n",
470+
"Summary:\n",
471+
"\n",
472+
"Time Complexity: O(n), since you scan through the list and then through the boolean array.\n",
473+
"Space Complexity: O(n), due to the extra boolean array."
294474
]
295475
},
296476
{
@@ -338,9 +518,9 @@
338518
],
339519
"metadata": {
340520
"kernelspec": {
341-
"display_name": "Python 3",
521+
"display_name": "Python (Pixi DSI)",
342522
"language": "python",
343-
"name": "python3"
523+
"name": "pixi-dsi"
344524
},
345525
"language_info": {
346526
"codemirror_mode": {
@@ -352,7 +532,7 @@
352532
"name": "python",
353533
"nbconvert_exporter": "python",
354534
"pygments_lexer": "ipython3",
355-
"version": "3.11.7"
535+
"version": "3.7.12"
356536
}
357537
},
358538
"nbformat": 4,

0 commit comments

Comments
 (0)