diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 19ce0c2..91665fd 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -21,9 +21,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ "import hashlib\n", "\n", @@ -31,9 +39,9 @@ " 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 = \"alejandro castellanos\"\n", "result = hash_to_range(input_string)\n", - "print(result)\n" + "print(result) # Output: 3\n" ] }, { @@ -80,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -90,8 +98,7 @@ "# self.val = val\n", "# self.left = left\n", "# self.right = right\n", - "def is_duplicate(root: TreeNode) -> int:\n", - " # TODO" + "# def is_duplicate(root: TreeNode) -> int:\n" ] }, { @@ -130,7 +137,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -140,8 +147,7 @@ "# self.val = val\n", "# self.left = left\n", "# self.right = right\n", - "def bt_path(root: TreeNode) -> List[List[int]]:\n", - " # TODO" + "# def bt_path(root: TreeNode) -> List[List[int]]:\n" ] }, { @@ -180,49 +186,152 @@ "#### Starter Code for Question 3\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Solving for missing numbers within the valid range" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "def missing_num(nums: List) -> int:\n", - " # TODO" + "def missing_num(nums):\n", + " # Find the largest number in the list\n", + " n = max(nums) # The largest number should be the upper bound\n", + " \n", + " # Create a set of the numbers in the list (automatically removes duplicates)\n", + " num_set = set(nums)\n", + " \n", + " # Generate the list of missing numbers\n", + " missing = [i for i in range(n + 1) if i not in num_set]\n", + " \n", + " # If no numbers are missing, return -1\n", + " if not missing:\n", + " return -1\n", + " \n", + " return missing\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-1\n", + "-1\n", + "[1]\n", + "[2, 3, 4]\n", + "[4, 9]\n" + ] + } + ], + "source": [ + "print(missing_num([0])) # Output: -1\n", + "print(missing_num([0, 1])) # Output: -1\n", + "print(missing_num([0, 2])) # Output: [1]\n", + "print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n", + "print(missing_num([6, 8, 2, 3, 5, 7, 0, 1, 10])) # Output: [4, 9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "\n", - "## Part 2:\n", - "\n", - "- Paraphrase the problem in your own words\n" + "### Importing List from typing" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Solving for missing numbers within the valid range" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "from typing import List # Import List from typing module\n", + "\n", + "def missing_num(nums: List[int]) -> int:\n", + " # Find the largest number in the list\n", + " n = max(nums) # The largest number should be the upper bound\n", + " \n", + " # Create a set of the numbers in the list (automatically removes duplicates)\n", + " num_set = set(nums)\n", + " \n", + " # Generate the list of missing numbers\n", + " missing = [i for i in range(n + 1) if i not in num_set]\n", + " \n", + " # If no numbers are missing, return -1\n", + " if not missing:\n", + " return -1\n", + " \n", + " return missing\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-1\n", + "-1\n", + "[1]\n", + "[2, 3, 4]\n", + "[4, 9]\n" + ] + } + ], + "source": [ + "print(missing_num([0])) # Output: -1\n", + "print(missing_num([0, 1])) # Output: -1\n", + "print(missing_num([0, 2])) # Output: [1]\n", + "print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n", + "print(missing_num([6, 8, 2, 3, 5, 7, 0, 1, 10])) # Output: [4, 9]\n" ] }, { "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" + "\n", + "## Part 2:\n", + "\n", + "- Paraphrase the problem in your own words\n" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "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", + "\n", + "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", + "\n", + "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" + ] + }, + { + "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" ] }, { @@ -235,11 +344,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "from typing import List\n", + "\n", + "def missing_num(nums: List[int]) -> int:\n", + " # Step 1: Find the maximum number in the list\n", + " n = max(nums)\n", + " \n", + " # Step 2: Create a set of all numbers from 0 to n\n", + " full_set = set(range(n + 1))\n", + " \n", + " # Step 3: Create a set from the input list\n", + " num_set = set(nums)\n", + " \n", + " # Step 4: Find the missing numbers\n", + " missing = list(full_set - num_set)\n", + " \n", + " # Step 5: Return the missing numbers or -1 if there are none\n", + " return missing if missing else -1\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4]\n", + "[1]\n", + "[1, 3, 6]\n", + "-1\n", + "-1\n" + ] + } + ], + "source": [ + "print(missing_num([5, 0, 1])) # Output: [2, 3, 4]\n", + "print(missing_num([0, 2, 2])) # Output: [1]\n", + "print(missing_num([8, 2, 0, 5, 7, 7, 4])) # Output: [1, 3, 6]\n", + "print(missing_num([0, 1])) # Output: -1\n", + "print(missing_num([0])) # Output: -1" ] }, { @@ -251,12 +401,18 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "Explanation:\n", + "\n", + "Full Set (full_set): This set contains all the numbers from 0 to the maximum number in the list (n).\n", + "\n", + "Input Set (num_set): We create a set from the input list to remove duplicates.\n", + "\n", + "Missing Numbers: By subtracting num_set from full_set, we get the missing numbers. \n", + "\n", + "If there are no missing numbers, we return -1." ] }, { @@ -268,12 +424,14 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "Time and Space Complexity:\n", + "\n", + "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", + "\n", + "Space Complexity: O(n), for the sets (full_set and num_set) used to store the numbers." ] }, { @@ -285,12 +443,34 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your answer here" + "Alternative Approach: Using a Boolean Array (Frequency Array) to Track Presence\n", + "\n", + "Determine the Range:\n", + "\n", + "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", + "Initialize a Boolean Array:\n", + "\n", + "Create a boolean array (or list) called seen with a length of max_value + 1.\n", + "Initialize every element to False. This array will track which numbers have been encountered.\n", + "Mark the Numbers Present:\n", + "\n", + "Iterate through the input list.\n", + "For each number num in the list, set seen[num] to True (this marks that the number num is present in the list).\n", + "Identify Missing Numbers:\n", + "\n", + "Iterate through the indices of the seen array (from 0 to max_value).\n", + "Collect the indices where the value remains False because these indices represent the numbers that were missing from the input list.\n", + "Return the Result:\n", + "\n", + "If you find any missing numbers, return the list of these numbers.\n", + "If no missing numbers are found (i.e., all indices are marked True), return -1.\n", + "Summary:\n", + "\n", + "Time Complexity: O(n), since you scan through the list and then through the boolean array.\n", + "Space Complexity: O(n), due to the extra boolean array." ] }, { @@ -338,9 +518,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python (Pixi DSI)", "language": "python", - "name": "python3" + "name": "pixi-dsi" }, "language_info": { "codemirror_mode": { @@ -352,7 +532,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.7.12" } }, "nbformat": 4,