{ "cells": [ { "cell_type": "markdown", "id": "7986f5aa-62a0-47b9-b8b0-e4ed0fe46729", "metadata": { "id": "7986f5aa-62a0-47b9-b8b0-e4ed0fe46729" }, "source": [ "# Set 2: Data Sense Presents: Python Zero to Hero Question Series (200 Questions)" ] }, { "cell_type": "markdown", "id": "952c9c83-df75-4d61-872a-33c924ee19c0", "metadata": { "id": "952c9c83-df75-4d61-872a-33c924ee19c0" }, "source": [ "**20 Questions**" ] }, { "cell_type": "markdown", "id": "9c205a07-2924-4a87-b064-b22c2491984e", "metadata": { "id": "9c205a07-2924-4a87-b064-b22c2491984e" }, "source": [ "**Question 1:** Create a Python program to count the number of digits in a given integer." ] }, { "cell_type": "code", "execution_count": null, "id": "bfee8a66-203e-40b5-8df4-5393a668d865", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bfee8a66-203e-40b5-8df4-5393a668d865", "outputId": "fa873f54-eaab-4c4a-c376-af96c9c62287" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "12345 contains 5 digits\n" ] } ], "source": [ "def count_digits_of_num(x):\n", " count = 0\n", " while int(x) > 0:\n", " count += 1\n", " x /= 10\n", "\n", " return count\n", "\n", "# main() code\n", "num=12345\n", "\n", "print(f\"{num} contains {count_digits_of_num(num)} digits\")" ] }, { "cell_type": "markdown", "id": "c82a2934-1b41-490a-8d7e-3df5be5cd0ca", "metadata": { "id": "c82a2934-1b41-490a-8d7e-3df5be5cd0ca" }, "source": [ "**Question 2:** Write a Python program using a for loop to count the frequency of each character in a given string." ] }, { "cell_type": "code", "execution_count": null, "id": "847be206-adcf-4814-af69-7058ad1720fc", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "847be206-adcf-4814-af69-7058ad1720fc", "outputId": "57042a84-8cba-4ffc-fa11-e116ee6542b6" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Frequency count of each character in 'Python Programming': \n", " {'P': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 2, ' ': 1, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 1}\n" ] } ], "source": [ "def count_frequency_of_character_in_str(sentence):\n", "\n", " character_frequency_count = {}\n", "\n", " for alphabet in sentence:\n", " if alphabet in character_frequency_count:\n", " character_frequency_count[alphabet] += 1\n", " else:\n", " character_frequency_count[alphabet] = 1\n", "\n", " return character_frequency_count\n", "\n", "\n", "# main() code\n", "input_str = \"Python Programming\"\n", "\n", "print(f\"Frequency count of each character in '{input_str}': \\n {count_frequency_of_character_in_str(input_str)}\")\n", "\n", "#Output: {'P': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 2, ' ': 1, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 1}" ] }, { "cell_type": "markdown", "id": "7ac47360-c8ce-411f-ba69-32e32e52cf83", "metadata": { "id": "7ac47360-c8ce-411f-ba69-32e32e52cf83" }, "source": [ "**Question 3**: Create a Python program using a nested for loop to print a pyramid pattern of stars. (based on input of number of rows)" ] }, { "cell_type": "code", "execution_count": null, "id": "ec5f3309-a722-4b7d-a512-0966090fb9e8", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ec5f3309-a722-4b7d-a512-0966090fb9e8", "outputId": "5c0d16d1-fb40-4b57-b1f3-e9da3e63d95e" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " *\n", " ***\n", " *****\n", " *******\n", " *********\n" ] } ], "source": [ "def pyramid_printing(n):\n", " star_sign = '*'\n", " counter = 1\n", " for row in range(n):\n", " for space in range(n - row):\n", " print(\" \", end='')\n", " for column in range(row+counter):\n", " print(star_sign, end='')\n", " print()\n", " counter += 1\n", "\n", "# main() code\n", "number_of_rows=5\n", "\n", "pyramid_printing(number_of_rows)\n" ] }, { "cell_type": "markdown", "id": "7e797708-5900-473c-81da-a710cca6725f", "metadata": { "id": "7e797708-5900-473c-81da-a710cca6725f" }, "source": [ "**Question 4**: Create a Python program using a for loop to print the first n odd numbers." ] }, { "cell_type": "code", "execution_count": null, "id": "2c96ef72-856c-4abf-9681-3863cd2af8a8", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2c96ef72-856c-4abf-9681-3863cd2af8a8", "outputId": "cb45201a-9326-4505-d1aa-b5be4bcf11da" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "First 50 odd numbers are : \n", " [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]\n" ] } ], "source": [ "def display_first_n_odd_nums(N):\n", " list_first_n_odd_nums = []\n", " number = 1\n", " for i in range(2*N):\n", " if number % 2 != 0:\n", " list_first_n_odd_nums.append(number)\n", " number += 1\n", " return list_first_n_odd_nums\n", "\n", "# main() code\n", "n=50\n", "\n", "print(f\"First {n} odd numbers are : \\n {display_first_n_odd_nums(n)}\")" ] }, { "cell_type": "markdown", "id": "70803cad-33e0-444c-9213-dc9973e06881", "metadata": { "id": "70803cad-33e0-444c-9213-dc9973e06881" }, "source": [ "**Question 5** Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:\n", "\n", "0 <= a, b, c, d < n\n", "\n", "a, b, c, and d are distinct.\n", "\n", "nums[a] + nums[b] + nums[c] + nums[d] == target\n", "\n", "You may return the answer in any order." ] }, { "cell_type": "markdown", "id": "33028fa4-0371-4fb7-b17f-0df2a1a40cea", "metadata": { "id": "33028fa4-0371-4fb7-b17f-0df2a1a40cea" }, "source": [ "**Example 1:**\n", "\n", "Input: nums = [1,0,-1,0,-2,2], target = 0\n", "\n", "Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]\n", "\n", "**Example 2:**\n", "\n", "Input: nums = [2,2,2,2,2], target = 8\n", "\n", "Output: [[2,2,2,2]]" ] }, { "cell_type": "code", "execution_count": null, "id": "9044f6d5-600a-4531-8b6f-eb33002a1e30", "metadata": { "id": "9044f6d5-600a-4531-8b6f-eb33002a1e30" }, "outputs": [], "source": [ "def find_unique_quadruplets_whose_sum_equals_target(N, x)\n", "\n", "# main() code\n", "nums = [1,0,-1,0,-2,2]\n", "target = 0\n", "\n", "print(f\"Unique Quadruplets of {nums} for making the sum of them as {target} are:\\n {find_unique_quadruplets_whose_sum_equals_target(nums, target)}\")\n", "\n", "# in-processing....!" ] }, { "cell_type": "markdown", "id": "5c31b7aa-d065-4edc-80e4-5d5e03ad68c8", "metadata": { "id": "5c31b7aa-d065-4edc-80e4-5d5e03ad68c8" }, "source": [ "**Question 6**: Given an integer array nums and an integer k, return the kth largest element in the array.\n", "\n", "Note that it is the kth largest element in the sorted order, not the kth distinct element.\n", "\n", "Can you solve it without sorting?\n", "\n", "\n", "\n", "Example 1:\n", "\n", "Input: nums = [3,2,1,5,6,4], k = 2\n", "Output: 5\n", "\n", "Example 2:\n", "\n", "Input: nums = [3,2,3,1,2,4,5,5,6], k = 4\n", "Output: 4" ] }, { "cell_type": "code", "source": [ "# temporary-manual-sorting\n", "def sort_the_list(LIST_ELE):\n", " temp = -1\n", " i = 0\n", " while i < len(LIST_ELE):\n", " if (temp > LIST_ELE[i]):\n", " value = temp\n", " temp = LIST_ELE[i]\n", " LIST_ELE[i] = value\n", " i += 1\n", "\n", " return LIST_ELE\n", "\n", "def kth_largest_element_in_numbers(N, K):\n", " list_to_store_k_elements = []\n", " i = 0\n", " while i < len(N):\n", " if (len(list_to_store_k_elements) <= K):\n", " list_to_store_k_elements.append(N[i])\n", " else:\n", " list_to_store_k_elements = sort_the_list(list_to_store_k_elements)\n", " list_to_store_k_elements.pop(0)\n", " if (N[i] > list_to_store_k_elements[0]):\n", " list_to_store_k_elements.append(N[i])\n", " i += 1\n", "\n", " return list_to_store_k_elements[(-1 * K)]\n", "\n", "# main() code\n", "print(\"Test Case 1 Running...\")\n", "nums = [3,2,1,5,6,4]\n", "k = 2\n", "print(f\"{kth_largest_element_in_numbers(nums, k)} is the {k}th largest element in {nums}\")\n", "print(\"Test Case 1 Passed!\")\n", "\n", "print()\n", "\n", "print(\"Test Case 2 Running...\")\n", "nums = [3,2,3,1,2,4,5,5,6]\n", "k = 4\n", "print(f\"{kth_largest_element_in_numbers(nums, k)} is the {k}th largest element in {nums}\")\n", "print(\"Test Case 2 Passed!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mPPD3RJVIPfB", "outputId": "4cbd16e6-ff85-4269-e984-9af4552323dc" }, "id": "mPPD3RJVIPfB", "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Test Case 1 Running...\n", "5 is the 2th largest element in [3, 2, 1, 5, 6, 4]\n", "Test Case 1 Passed!\n", "\n", "Test Case 2 Running...\n", "4 is the 4th largest element in [3, 2, 3, 1, 2, 4, 5, 5, 6]\n", "Test Case 2 Passed!\n" ] } ] }, { "cell_type": "markdown", "id": "c7e8e37a-5e1b-4f9d-a43d-8c96c5d57d70", "metadata": { "id": "c7e8e37a-5e1b-4f9d-a43d-8c96c5d57d70" }, "source": [ "**Question 7**: Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.\n", "\n", "\n", "\n", "Example 1:\n", "\n", "Input: nums = [1,1,1,2,2,3], k = 2\n", "Output: [1,2]\n", "\n", "Example 2:\n", "\n", "Input: nums = [1], k = 1\n", "Output: [1]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9b6c352f-a0c4-4bfb-84a8-1242257b0354", "metadata": { "id": "9b6c352f-a0c4-4bfb-84a8-1242257b0354", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "bdbed9d8-983c-47a7-8f27-c0d8f5027520" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2 most frequest elements in [1, 1, 1, 2, 2, 3] are: [1, 2]\n" ] } ], "source": [ "def find_k_most_frequent_numbers(N, K):\n", " # dictionary to store key-value pair of number and its frequency/count\n", " temp_frequency_dictionary = {}\n", " # list to store most frequent elements/numbers\n", " k_frequent_elements = []\n", "\n", " # find the frequency of each element/number and storing into dictionary with key-value pair of number-count\n", " for element in N:\n", " if element in temp_frequency_dictionary:\n", " temp_frequency_dictionary[element] += 1\n", " else:\n", " temp_frequency_dictionary[element] = 1\n", "\n", " # using list comprehension, making a list of frequency/count of each numbers\n", " list_frequency_count = [count for ele,count in temp_frequency_dictionary.items()]\n", " # sorting the list of frequency/count in descending order\n", " list_frequency_count.sort(reverse = True)\n", "\n", " # iterating over the list from start to Kth position (using list slicing)\n", " for item in list_frequency_count[:K]:\n", " # iterating over the dictionary to match the frequency count in list with frequency count in values of key-value pairs and appending it into list of most frequent elements/numbers\n", " for ele in temp_frequency_dictionary.keys():\n", " if temp_frequency_dictionary[ele] == item:\n", " k_frequent_elements.append(ele)\n", "\n", " # returning the list of most frequent elements/numbers\n", " return k_frequent_elements\n", "\n", "# main() code\n", "nums = [1,1,1,2,2,3]\n", "k = 2\n", "\n", "print(f\"{k} most frequest elements in {nums} are: {find_k_most_frequent_numbers(nums, k)}\")" ] }, { "cell_type": "markdown", "id": "f737efad-e83d-4f27-b424-6bca40d35633", "metadata": { "id": "f737efad-e83d-4f27-b424-6bca40d35633" }, "source": [ "**Question 8**: Write a Python program to remove all instances of a specific value in-place from a list. Return the new length of the list." ] }, { "cell_type": "code", "execution_count": null, "id": "f31f8ddd-f697-451c-804c-c3877c320ec2", "metadata": { "id": "f31f8ddd-f697-451c-804c-c3877c320ec2", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "531480ae-5a8c-458e-8917-92b7112b2329" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Modified List: [2, 2, 4, 2]\n", "Length of modified list: 4\n" ] } ], "source": [ "def removing_specified_value(N, x):\n", " # solution 1\n", " count_of_value = N.count(x)\n", " for i in range(count_of_value):\n", " N.remove(x)\n", " return N\n", "\n", " # # solution 2\n", " # while True:\n", " # if x in N:\n", " # N.remove(x)\n", " # else:\n", " # break\n", " # return N\n", "\n", "# main() code\n", "nums = [3, 2, 2, 3, 4, 2, 3]\n", "val = 3\n", "\n", "nums = removing_specified_value(nums, val)\n", "print(f\"Modified List: {nums}\\nLength of modified list: {len(nums)}\")\n", "#output\n", "#The new length of the list is: 4\n", "#The modified list is: [2, 2, 4, 2]\n" ] }, { "cell_type": "markdown", "id": "b26369dc-2ecd-4587-bd2e-a8aeeef7224c", "metadata": { "id": "b26369dc-2ecd-4587-bd2e-a8aeeef7224c" }, "source": [ "**Question 9:** Write a Python program to find the length of the longest substring without repeating characters" ] }, { "cell_type": "markdown", "id": "e8e5d512-7b8f-4f4c-af72-9119b9f38866", "metadata": { "id": "e8e5d512-7b8f-4f4c-af72-9119b9f38866" }, "source": [ "s = \"abcabcbb\"\n", "\n", "Input String: \"abcabcbb\"\n", "\n", "Possible Substrings without Repeating Characters:\n", "\n", "\"abc\" (length 3)\n", "\n", "\"bca\" (length 3)\n", "\n", "\"cab\" (length 3)\n", "\n", "\"abc\" (length 3)\n", "\n", "\"b\" (length 1)\n", "\n", "Longest Substring without Repeating Characters: \"abc\" (length 3)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1cdd771c-ef07-4a9f-8585-90f39abba5c3", "metadata": { "id": "1cdd771c-ef07-4a9f-8585-90f39abba5c3", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e0f5c5a1-ce5e-415d-fd99-e0ee14d56381" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Length of the longest substring without repeating characters in abcabcbb is : 3\n" ] } ], "source": [ "def length_of_longest_substring_without_repeating_characters(sentence):\n", " # pointer to manage the position in sentence while iteration\n", " i = j = 0\n", " # string to store the longest sub-string without repeating characters\n", " long_sub_str = ''\n", " # iterating over sentence from start to end\n", " while (i < len(sentence)):\n", " # temporary string to store sub-string without repeating characters\n", " sub_str = ''\n", " # iterating over the sentence from to end-of-sentence\n", " # Note: The reason for starting from is, that every time when starting to iterate over sentence, no need to again iterate to same values rather-than start from where the loop was break (last sub-string without repeating characters was ended)\n", " for j in range(i,len(sentence)):\n", " # checking if character (sentence[j]) exist in temporary string or not\n", " # if it is not present in temporary string, than add it to temporary string\n", " if sentence[j] not in sub_str:\n", " sub_str += sentence[j]\n", " # else if character is already present in temporary string, means the pointer reaches to repeating character. So simply break the loop (for loop) and add the value of lastly visited index in sentence to \n", " else:\n", " i += j\n", " break\n", " # check if the length of temporary string is greater than or equal to count\n", " # if it is, than assign that temporary string to the long_sub_string variable\n", " if (len(sub_str) >= len(long_sub_str)):\n", " long_sub_str = sub_str\n", " # return the length of longest sub-string without repeating characters\n", " return len(long_sub_str)\n", "\n", "\n", "# main() code\n", "s = \"abcabcbb\"\n", "\n", "print(f\"Length of the longest substring without repeating characters in {s} is : {length_of_longest_substring_without_repeating_characters(s)}\")\n", "#Output: Length of the longest substring without repeating characters is: 3\n" ] }, { "cell_type": "markdown", "id": "dd7012c7-7510-4f84-90da-bdca802c99bc", "metadata": { "id": "dd7012c7-7510-4f84-90da-bdca802c99bc" }, "source": [ "**Question 10** Write a Python program to merge two arrays into a single sorted array\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "41c2dbc6-721a-43ad-9e9e-073595dc3749", "metadata": { "id": "41c2dbc6-721a-43ad-9e9e-073595dc3749", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e90af205-b5fa-475e-8512-14e96352d6f2" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Merged Array in sorted order: [1, 2, 3, 4, 5, 6, 7, 8]\n" ] } ], "source": [ "def merge_two_aarays(A, B):\n", " # declaring a new array/list to merge two arrays in sorted order\n", " two_array_sorted = []\n", " # taking two pointers, one for each array\n", " i = j = 0\n", "\n", " # iterating over each array/list using pointer and sorting them and appending it to declared array (two_array_sorted)\n", " while ((i < len(A)) and (j < len(B))):\n", "\n", " if A[i] <= B[j]:\n", " two_array_sorted.append(A[i])\n", " i +=1\n", " else:\n", " two_array_sorted.append(B[j])\n", " j += 1\n", "\n", " # iterating over the first array/list, in-case if any element gets remain to iterate over (only happens if initially second array/list would have completely iterated before first array/list)\n", " while (i < len(A)):\n", " two_array_sorted.append(A[i])\n", " i += 1\n", "\n", " # iterating over the second array/list, in-case if any element gets remain to iterate over (only happens if initially first array/list would have completely iterated before second array/list)\n", " while (j < len(B)):\n", " two_array_sorted.append(B[j])\n", " j +=1\n", "\n", " return two_array_sorted\n", "\n", "\n", "# main() code\n", "arr1 = [1, 3, 5, 7]\n", "arr2 = [2, 4, 6, 8]\n", "\n", "merge_arrays = merge_two_aarays(arr1, arr2)\n", "\n", "print(f\"Merged Array in sorted order: {merge_arrays}\")\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 5 }