|
21 | 21 | },
|
22 | 22 | {
|
23 | 23 | "cell_type": "code",
|
24 |
| - "execution_count": null, |
| 24 | + "execution_count": 1, |
25 | 25 | "metadata": {},
|
26 |
| - "outputs": [], |
| 26 | + "outputs": [ |
| 27 | + { |
| 28 | + "name": "stdout", |
| 29 | + "output_type": "stream", |
| 30 | + "text": [ |
| 31 | + "3\n" |
| 32 | + ] |
| 33 | + } |
| 34 | + ], |
27 | 35 | "source": [
|
28 | 36 | "import hashlib\n",
|
29 | 37 | "\n",
|
30 | 38 | "def hash_to_range(input_string: str) -> int:\n",
|
31 | 39 | " hash_object = hashlib.sha256(input_string.encode())\n",
|
32 | 40 | " hash_int = int(hash_object.hexdigest(), 16)\n",
|
33 | 41 | " return (hash_int % 3) + 1\n",
|
34 |
| - "input_string = \"your_first_name_here\"\n", |
| 42 | + "input_string = \"alejandro castellanos\"\n", |
35 | 43 | "result = hash_to_range(input_string)\n",
|
36 |
| - "print(result)\n" |
| 44 | + "print(result) # Output: 3\n" |
37 | 45 | ]
|
38 | 46 | },
|
39 | 47 | {
|
|
80 | 88 | },
|
81 | 89 | {
|
82 | 90 | "cell_type": "code",
|
83 |
| - "execution_count": null, |
| 91 | + "execution_count": 2, |
84 | 92 | "metadata": {},
|
85 | 93 | "outputs": [],
|
86 | 94 | "source": [
|
|
90 | 98 | "# self.val = val\n",
|
91 | 99 | "# self.left = left\n",
|
92 | 100 | "# self.right = right\n",
|
93 |
| - "def is_duplicate(root: TreeNode) -> int:\n", |
94 |
| - " # TODO" |
| 101 | + "# def is_duplicate(root: TreeNode) -> int:\n" |
95 | 102 | ]
|
96 | 103 | },
|
97 | 104 | {
|
|
130 | 137 | },
|
131 | 138 | {
|
132 | 139 | "cell_type": "code",
|
133 |
| - "execution_count": null, |
| 140 | + "execution_count": 3, |
134 | 141 | "metadata": {},
|
135 | 142 | "outputs": [],
|
136 | 143 | "source": [
|
|
140 | 147 | "# self.val = val\n",
|
141 | 148 | "# self.left = left\n",
|
142 | 149 | "# 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" |
145 | 151 | ]
|
146 | 152 | },
|
147 | 153 | {
|
|
180 | 186 | "#### Starter Code for Question 3\n"
|
181 | 187 | ]
|
182 | 188 | },
|
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "metadata": {}, |
| 192 | + "source": [ |
| 193 | + "### Solving for missing numbers within the valid range" |
| 194 | + ] |
| 195 | + }, |
183 | 196 | {
|
184 | 197 | "cell_type": "code",
|
185 |
| - "execution_count": null, |
| 198 | + "execution_count": 4, |
186 | 199 | "metadata": {},
|
187 | 200 | "outputs": [],
|
188 | 201 | "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]" |
191 | 243 | ]
|
192 | 244 | },
|
193 | 245 | {
|
194 | 246 | "cell_type": "markdown",
|
195 | 247 | "metadata": {},
|
196 | 248 | "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" |
201 | 257 | ]
|
202 | 258 | },
|
203 | 259 | {
|
204 | 260 | "cell_type": "code",
|
205 |
| - "execution_count": null, |
| 261 | + "execution_count": 6, |
206 | 262 | "metadata": {},
|
207 | 263 | "outputs": [],
|
208 | 264 | "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" |
210 | 307 | ]
|
211 | 308 | },
|
212 | 309 | {
|
213 | 310 | "cell_type": "markdown",
|
214 | 311 | "metadata": {},
|
215 | 312 | "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" |
217 | 317 | ]
|
218 | 318 | },
|
219 | 319 | {
|
220 |
| - "cell_type": "code", |
221 |
| - "execution_count": null, |
| 320 | + "cell_type": "markdown", |
222 | 321 | "metadata": {},
|
223 |
| - "outputs": [], |
224 | 322 | "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" |
226 | 335 | ]
|
227 | 336 | },
|
228 | 337 | {
|
|
235 | 344 | },
|
236 | 345 | {
|
237 | 346 | "cell_type": "code",
|
238 |
| - "execution_count": null, |
| 347 | + "execution_count": 8, |
239 | 348 | "metadata": {},
|
240 | 349 | "outputs": [],
|
241 | 350 | "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" |
243 | 393 | ]
|
244 | 394 | },
|
245 | 395 | {
|
|
251 | 401 | ]
|
252 | 402 | },
|
253 | 403 | {
|
254 |
| - "cell_type": "code", |
255 |
| - "execution_count": null, |
| 404 | + "cell_type": "markdown", |
256 | 405 | "metadata": {},
|
257 |
| - "outputs": [], |
258 | 406 | "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." |
260 | 416 | ]
|
261 | 417 | },
|
262 | 418 | {
|
|
268 | 424 | ]
|
269 | 425 | },
|
270 | 426 | {
|
271 |
| - "cell_type": "code", |
272 |
| - "execution_count": null, |
| 427 | + "cell_type": "markdown", |
273 | 428 | "metadata": {},
|
274 |
| - "outputs": [], |
275 | 429 | "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." |
277 | 435 | ]
|
278 | 436 | },
|
279 | 437 | {
|
|
285 | 443 | ]
|
286 | 444 | },
|
287 | 445 | {
|
288 |
| - "cell_type": "code", |
289 |
| - "execution_count": null, |
| 446 | + "cell_type": "markdown", |
290 | 447 | "metadata": {},
|
291 |
| - "outputs": [], |
292 | 448 | "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." |
294 | 474 | ]
|
295 | 475 | },
|
296 | 476 | {
|
|
338 | 518 | ],
|
339 | 519 | "metadata": {
|
340 | 520 | "kernelspec": {
|
341 |
| - "display_name": "Python 3", |
| 521 | + "display_name": "Python (Pixi DSI)", |
342 | 522 | "language": "python",
|
343 |
| - "name": "python3" |
| 523 | + "name": "pixi-dsi" |
344 | 524 | },
|
345 | 525 | "language_info": {
|
346 | 526 | "codemirror_mode": {
|
|
352 | 532 | "name": "python",
|
353 | 533 | "nbconvert_exporter": "python",
|
354 | 534 | "pygments_lexer": "ipython3",
|
355 |
| - "version": "3.11.7" |
| 535 | + "version": "3.7.12" |
356 | 536 | }
|
357 | 537 | },
|
358 | 538 | "nbformat": 4,
|
|
0 commit comments