diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 26bb3864..87ba8b31 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -27,9 +27,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", @@ -37,7 +45,7 @@ " 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 = \"ashwinder\"\n", "result = hash_to_range(input_string)\n", "print(result)\n" ] @@ -159,7 +167,7 @@ "\n", " # Question Three: Missing Number in Range\n", " \n", - " You are given a list containing `n` integers in the range `[0, n]`. Return a list of numbers that are missing from the range `[0, n]` of the array. If there is no missing number, return -1. Note, all the integers in the list may not be unique.\n", + " You are given a list of non-negative integers. Return a list of numbers that are missing from the range [0, maximum value]. If there is no missing number, return -1. Note, all the integers in the list may not be unique.\n", " \n", " ## Examples\n", "\n", @@ -188,14 +196,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[2, 3, 4]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "from typing import List\n", + "\n", "def missing_num(nums: List) -> int:\n", - " # TODO" + " #todo\n", + " if len(nums) == 0:\n", + " return -1\n", + " \n", + " # take the maximum value to create a bool present list\n", + " max_number = max(nums)\n", + " present = [False] * (max_number + 1)\n", + " \n", + " # now set the present list indices true for the values present in nums\n", + " for num in nums:\n", + " if 0 <= num <= max_number:\n", + " present[num] = True\n", + "\n", + " # collect the missing numbers\n", + " missing = []\n", + " # to collect missing number\n", + " # check for the range 0 to max\n", + " for i in range(max_number + 1):\n", + " if present[i] != True:\n", + " missing.append(i)\n", + "\n", + " return missing\n", + "\n", + "nums = [5,0,1]\n", + "missing_num(nums)" ] }, { @@ -209,6 +254,15 @@ "You and your partner must share each other's Assignment 1 submission." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I got the assignment below:\n", + "\n", + "https://github.com/VinuShan4/algorithms_and_data_structures/pulls" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -396,7 +450,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "dsi_participant", "language": "python", "name": "python3" }, @@ -410,7 +464,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.9.23" } }, "nbformat": 4,