diff --git a/02_activities/assignments/assignment_1.ipynb b/02_activities/assignments/assignment_1.ipynb index 15a2c12..685a72f 100644 --- a/02_activities/assignments/assignment_1.ipynb +++ b/02_activities/assignments/assignment_1.ipynb @@ -21,11 +21,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], "source": [ - "print((hash('your first name') % 3) + 1)" + "print((hash('shiyam') % 3) + 1)" ] }, { @@ -174,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -198,7 +206,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "#Given a list of integers find the all integers which are missing from the list " ] }, { @@ -215,7 +224,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "lst2 = [0,10] # expected retun [1,2,3,4,5,6,7,8,9]\n", + "lst3 = [4,5] # expected result [0,1,2,3]\n" ] }, { @@ -228,11 +239,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "from typing import List\n", + "\n", + "def missing_num(nums: List[int]) -> List[int]:\n", + " n = max(nums)\n", + " \n", + " def missing_num_helper(current: int, missing: List[int]) -> List[int]:\n", + " if current > n:\n", + " return missing if missing else [-1]\n", + " \n", + " if current not in nums:\n", + " missing.append(current)\n", + " \n", + " return missing_num_helper(current + 1, missing)\n", + " \n", + " return missing_num_helper(0, [])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1]\n", + "[2, 3, 4]\n", + "[4, 9]\n", + "[-1]\n" + ] + } + ], + "source": [ + "\n", + "# Test cases\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", + "print(missing_num([0, 1, 2, 3])) # Output: [-1]" ] }, { @@ -249,7 +299,15 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# We define the missing function with as per the suggested signature \n", + "# the max value in the list is found and stored in a varibale called n\n", + "# a helper recursive function is defined which takes the currect integer\n", + "# we are checking and list to store the missing values as input parameter \n", + "# in the helper function we return the list of missing values if cuurent \n", + "# integer is greater than n or -1 is missing list is empty \n", + "# if current is not in the input list, we add it to missing list\n", + "# we recursivally call the missing_list_helper with current+1 to check the next number " ] }, { @@ -262,11 +320,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "# time complexity of the program will be O(n) as the code \n", + "# runs at most the max number of element in the given list\n", + "\n", + "# space complexity will also be O(n) as the depth of recursive function will be n " ] }, { @@ -283,7 +345,19 @@ "metadata": {}, "outputs": [], "source": [ - "# Your answer here" + "# Your answer here\n", + "def missing_num_alt(nums):\n", + " n = max(nums)\n", + " full_set = set(range(n))\n", + " num_set = set(nums)\n", + " missing_set = list(full_set - num_set)\n", + "\n", + " if not missing_set:\n", + " return -1\n", + " \n", + " return sorted(missing_set)\n", + "\n", + " #using the property of the set we can achive the required output with an O(1) time complexity " ] }, { @@ -345,7 +419,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.7" + "version": "3.9.15" } }, "nbformat": 4,