Skip to content

Commit

Permalink
Assignment 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Veronika Plonsak committed Jul 24, 2024
1 parent 6fd2944 commit 7e5acd0
Showing 1 changed file with 89 additions and 34 deletions.
123 changes: 89 additions & 34 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"print((hash('your first name') % 3) + 1)"
"print((hash('Veronika') % 3) + 1)"
]
},
{
Expand Down Expand Up @@ -172,16 +180,6 @@
"#### Starter Code for Question 3\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def missing_num(nums: List) -> int:\n",
" # TODO"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -193,12 +191,12 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"I believe the problem states that we have n integers in the range [0, n], implying that the largest number in the given list is equal to the number of integers in the list, but this is not the case for the provided examples. A correct statement should involve two unknown variables: m and n.\n",
"\n",
"In other words, we have an unsorted list of m integers, which may include duplicates. The maximum number in the list is n, but we do not know its value or its position. We need to determine if all integers in the range [0, n] appear at least once in the given list and return any missing values. If there are no missing values, we should return -1."
]
},
{
Expand All @@ -210,11 +208,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"# Example \n",
"lst_4 = [12,2,8,8,5,1,1,1]\n",
"output_4 = [2, 4, 6, 7, 9, 10, 12, 15]\n",
"\n",
"# Example \n",
"lst_5 = [5,4,3,2,1]\n",
"output_5 = -1\n"
]
},
{
Expand All @@ -227,11 +231,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"def missing_num(nums: list): \n",
" maximum = max(nums) \n",
" set_from_list = set(nums)\n",
" set_from_range = set(range(0,maximum))\n",
" diff = set_from_range - set_from_list\n",
" \n",
" if len(diff)>0:\n",
" return list(diff)\n",
" else:\n",
" return -1"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"output 1: [1]\n",
"output 2: [2, 3, 4]\n",
"output 3: [9, 4]\n",
"output 4: [0, 3, 4, 6, 7, 9, 10, 11]\n",
"output 5: -1\n"
]
}
],
"source": [
"lst_1 = [0,2]\n",
"lst_2 = [5,0,1]\n",
"lst_3 = [6, 8, 2, 3, 5, 7, 0, 1, 10]\n",
"lst_4 = [12,2,8,8,5,1,1,1]\n",
"lst_5 = [5,4,3,2,1,0]\n",
"lists = [lst_1,lst_2,lst_3,lst_4,lst_5]\n",
"for i in range(len(lists)):\n",
" print(f'output {i+1}: ',missing_num(lists[i]))"
]
},
{
Expand All @@ -243,12 +284,23 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The goal is to identify missing numbers in each list within the range from 0 to the maximum value present in the list. The solution works as follows:\n",
"Since sets disallow duplicates and lack element order, they are ideal for this problem. To solve it, find the maximum value in the list, convert the list to a set, and create another set with values from 0 to the maximum. Then, subtract these sets.\n",
"\n",
"\n",
"1. First, we find the maximum number in the given list, which defines the range of numbers we need to check (from 0 to this maximum number).\n",
"\n",
"2. By converting the given list to a set, we handle duplicates automatically since sets do not allow duplicate values.\n",
"\n",
"3. We create a set that contains all numbers from 0 to the maximum number found in the list.\n",
"\n",
"4. We compute the difference between the full set and the set created from the list. This difference gives us the missing numbers.\n",
"\n",
"5. If the difference set is non-empty, we return it as a list of missing values. If it is empty, it means all numbers in the range are present in the list, so we return -1.\n",
"\n"
]
},
{
Expand All @@ -260,12 +312,11 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"The problem's time complexity is O(n + m) because the operations involving sets and lists have this combined complexity, and there are no nested loops that would increase the complexity.\n",
"The problem's space complexity is O(n + m) because the space required for storing either the list or the set scales linearly with the size of the list and the range of numbers up to the maximum value."
]
},
{
Expand All @@ -277,12 +328,16 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
"1. Find the maximum number in the list\n",
"2. Iterate through the list to find the maximum value. \n",
"3. Create a boolean list of size (max + 1), initialized with False. \n",
"4. Mark present numbers\n",
"5. Identify missing numbers\n",
"6. Return the result\n",
"\n"
]
},
{
Expand Down Expand Up @@ -344,7 +399,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
"version": "3.9.18"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 7e5acd0

Please sign in to comment.