From d72b41077e1ec45622c16cf026ff2e2cdf7f6202 Mon Sep 17 00:00:00 2001 From: mcodevb <57311970+mcodevb@users.noreply.github.com> Date: Sat, 1 Jul 2023 15:32:12 +0530 Subject: [PATCH] Created using Colaboratory --- Assignment/5/Assignment_5_Ritwik_Saha.ipynb | 1658 +++++++++++++++++++ 1 file changed, 1658 insertions(+) create mode 100644 Assignment/5/Assignment_5_Ritwik_Saha.ipynb diff --git a/Assignment/5/Assignment_5_Ritwik_Saha.ipynb b/Assignment/5/Assignment_5_Ritwik_Saha.ipynb new file mode 100644 index 0000000000..cb8ef3f7b9 --- /dev/null +++ b/Assignment/5/Assignment_5_Ritwik_Saha.ipynb @@ -0,0 +1,1658 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "id": "d3c758ce", + "metadata": { + "id": "d3c758ce" + }, + "source": [ + "# Some problems on map and filter" + ] + }, + { + "cell_type": "markdown", + "id": "3673e898", + "metadata": { + "id": "3673e898" + }, + "source": [ + "### Q1. Given a list of strings, use map to convert each string to uppercase." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69e680f0", + "metadata": { + "id": "69e680f0" + }, + "outputs": [], + "source": [ + "def convert_to_uppercase(string):\n", + " uppercase_of_string = string.upper()\n", + " return uppercase_of_string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8cc27ae6", + "metadata": { + "id": "8cc27ae6", + "outputId": "587797c7-ba83-4f23-d0a4-07ec9fe0baa6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'RITWIK SAHA'" + ] + }, + "execution_count": 174, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "convert_to_uppercase('ritwIK saha')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5be7c32c", + "metadata": { + "id": "5be7c32c", + "outputId": "6a61517f-c288-4428-d749-8082eb00fe0a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('RITWIK', 'SAHA', 'RAMPURHAT', 'VISVA BHARATI')" + ] + }, + "execution_count": 175, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tuple(map(convert_to_uppercase, ['ritwik', 'saha', 'rampurhat', 'visva bharati']))" + ] + }, + { + "cell_type": "markdown", + "id": "b0772edf", + "metadata": { + "id": "b0772edf" + }, + "source": [ + "### Q2. Given a list of numbers, use filter to find all multiples of 3." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd921a04", + "metadata": { + "id": "dd921a04" + }, + "outputs": [], + "source": [ + "def is_multiple_of_3(n):\n", + " if (n%3 == 0):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10e387c2", + "metadata": { + "id": "10e387c2", + "outputId": "3e57fdfe-ed8d-4d82-db30-cab69f01162c" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "is_multiple_of_3(17)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64c43575", + "metadata": { + "id": "64c43575", + "outputId": "9d6f3b03-1795-4ccc-e96a-69b2eb44a864" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[-15, -6, 0, 3, 9, 12, 15]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(is_multiple_of_3, [-15,-6,0,1,2,3,5,7,9,12,15,19,20]))" + ] + }, + { + "cell_type": "markdown", + "id": "59ce7f55", + "metadata": { + "id": "59ce7f55" + }, + "source": [ + "### Q3. Given a list of strings, use filter to find all the strings that have more than 5 characters.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7231254", + "metadata": { + "id": "a7231254" + }, + "outputs": [], + "source": [ + "def character_greaterthan5(string):\n", + " if len(string)>5:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bb4280c1", + "metadata": { + "id": "bb4280c1", + "outputId": "3bb5e924-0fb2-4089-cb65-f94ee43d4460" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "character_greaterthan5('RitwikSaha')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4645da75", + "metadata": { + "id": "4645da75", + "outputId": "db5ce763-23fb-4651-96aa-22fdb1dc2dc8" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['helloo', 'ritwik', 'youuuuu']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(character_greaterthan5, ['hii', 'helloo', 'ritwik', 'saha', 'youuuuu']))" + ] + }, + { + "cell_type": "markdown", + "id": "8517d264", + "metadata": { + "id": "8517d264" + }, + "source": [ + "### Q4. Given a list of tuples (name, age), use map and filter to find the names of people who are above 18 years old." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a69a21f1", + "metadata": { + "id": "a69a21f1" + }, + "outputs": [], + "source": [ + "def people_above_18years(name_and_age):\n", + " if name_and_age[1]>18:\n", + "# print(name_and_age[0])\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a9187f00", + "metadata": { + "id": "a9187f00", + "outputId": "0fa06827-04cc-426b-9601-b730c0cce0ac" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people_above_18years(('Ritwik', 23))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2596c7ec", + "metadata": { + "id": "2596c7ec", + "outputId": "c1477336-85a7-4f07-81ce-6e0448478233" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ritwik\n", + "Rakesh\n" + ] + } + ], + "source": [ + "list1=list(filter(people_above_18years, [('Ritwik', 23), ('Rakesh', 19), ('Sam', 12), ('Manu', 15)]))\n", + "\n", + "for x in list1:\n", + " print(x[0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b2fa481", + "metadata": { + "id": "6b2fa481" + }, + "outputs": [], + "source": [ + "# list(map(people_above_18years, [('Ritwik', 23), ('Rakesh', 19), ('Sam', 12), ('Manu', 15)]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "572a0a64", + "metadata": { + "id": "572a0a64" + }, + "outputs": [], + "source": [ + "# list(filter(people_above_18years, [('Ritwik', 23), ('Rakesh', 19), ('Sam', 12), ('Manu', 15)]))" + ] + }, + { + "cell_type": "markdown", + "id": "4532d05b", + "metadata": { + "id": "4532d05b" + }, + "source": [ + "### Q5. Given a list of dictionaries representing people (each dictionary contains keys 'name' and 'age'), use filter to find all the people whose age is above 30.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d09953b4", + "metadata": { + "id": "d09953b4" + }, + "outputs": [], + "source": [ + "def people_above_30years(name_and_age):\n", + "# name = name_and_age.keys()\n", + "# age = name_and_age.values()\n", + " name_list = list(name_and_age.keys())\n", + " age_list = list(name_and_age.values())\n", + "\n", + "# print(name_list)\n", + "# print(age_list)\n", + "\n", + " if age_list[0]>30:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20fb4c50", + "metadata": { + "id": "20fb4c50", + "outputId": "88ac7f37-a669-4f3a-f774-47cdbeee02b9" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "people_above_30years({'Ram': 35})" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b22f2d44", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 235 + }, + "id": "b22f2d44", + "outputId": "80641071-ff1e-4d38-a854-14da71294240" + }, + "outputs": [ + { + "output_type": "error", + "ename": "NameError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpeople_above_30years\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;34m'Ritwik'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m38\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'manu'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m12\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'kajal'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m55\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m)\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mans_list\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlst\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlist1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mans_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mNameError\u001b[0m: name 'people_above_30years' is not defined" + ] + } + ], + "source": [ + "list1 = list(filter(people_above_30years, [{'Ritwik': 38}, {'manu':12}, {'kajal':55}] ) )\n", + "ans_list=[]\n", + "\n", + "for lst in list1:\n", + " ans_list.append(lst.keys())\n", + "\n", + "# print(ans_list)\n", + "answer = []\n", + "\n", + "for x in ans_list:\n", + " answer.append(list(x))\n", + "\n", + "print(answer)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27ca3bd4", + "metadata": { + "id": "27ca3bd4" + }, + "outputs": [], + "source": [ + "# list3=list(map(people_above_30years, [{'Ritwik': 38}, {'manu':12}] ) )\n", + "\n", + "# print(list2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "160d6abe", + "metadata": { + "id": "160d6abe" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84488990", + "metadata": { + "id": "84488990" + }, + "outputs": [], + "source": [ + "# name_and_age = {'Ritwik': 38, 'manu':12}\n", + "# type(name_and_age)\n", + "# # name_and_age['Ritwik']>30\n", + "# # len(name_and_age)\n", + "# x=name_and_age.keys()\n", + "# # print(x)\n", + "# lst=list(x)\n", + "# print(lst)" + ] + }, + { + "cell_type": "markdown", + "id": "ceac2f84", + "metadata": { + "id": "ceac2f84" + }, + "source": [ + "### Q6. Given a list of strings, use map to convert each string to its length." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73fe0b9d", + "metadata": { + "id": "73fe0b9d" + }, + "outputs": [], + "source": [ + "def find_length(string):\n", + " length_of_the_string = len(string)\n", + " return length_of_the_string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9cc4eaf3", + "metadata": { + "id": "9cc4eaf3", + "outputId": "70e96c5f-c279-42bc-e22d-99b58fa74ad0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "find_length('Ramesh')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "99a16952", + "metadata": { + "id": "99a16952", + "outputId": "7cc5443e-d409-435a-d205-c26f776658c6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 6, 7, 5]" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(find_length, ['Ramesh', 'Ritwik', 'Shyamal', 'Kamal']))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6b6f5b6", + "metadata": { + "id": "c6b6f5b6" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "cd87b29e", + "metadata": { + "id": "cd87b29e" + }, + "source": [ + "### Q7. Given a list of integers, use filter to find all the numbers which are one less than some prime numbers." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "484fc720", + "metadata": { + "id": "484fc720" + }, + "outputs": [], + "source": [ + "def is_prime(n):\n", + " n=n+1 #Here I increase the value of n by 1 to check the number (n+1) is prime or not\n", + " factor=[]\n", + " for i in range(1,n+1):\n", + " if(n%i == 0):\n", + " factor.append(i)\n", + "\n", + " if(len(factor)<=2):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3a003824", + "metadata": { + "id": "3a003824", + "outputId": "33e8deae-397b-4fec-d2ad-3fcdc54ae9af" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The numbers which are less than some prime number from the list [4, 5, 6, 8, 10, 13, 17, 18, 22, 30] are: [4, 6, 10, 18, 22, 30]\n" + ] + } + ], + "source": [ + "list1=[4,5,6,8,10,13,17,18,22,30]\n", + "print(f'The numbers which are less than some prime number from the list {list1} are: {list(filter(is_prime, list1))}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4fac020f", + "metadata": { + "id": "4fac020f" + }, + "outputs": [], + "source": [ + "# list(filter(is_prime, list1))\n", + "\n", + "# list2=[]\n", + "# for x in list1:\n", + "# list2.append(x+1)\n", + "\n", + "\n", + "# prime_list=list(filter(is_prime, list2))\n", + "# # print(prime_list)\n", + "# list3=[]\n", + "# for y in prime_list:\n", + "# list3.append(y-1)\n", + "\n", + "# list(filter(is_prime, list1))" + ] + }, + { + "cell_type": "markdown", + "id": "61ba3f5f", + "metadata": { + "id": "61ba3f5f" + }, + "source": [ + "### Q8. Given a list of dictionaries representing products (each dictionary contains keys 'name' and 'price'), use filter to find all the products with a price less than $10." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1128c8fe", + "metadata": { + "id": "1128c8fe" + }, + "outputs": [], + "source": [ + "def product_price_lessthan10(dictionary_name_price):\n", + " name = list(dictionary_name_price.keys())\n", + " price = list(dictionary_name_price.values())\n", + "\n", + " for x in price:\n", + " if x < 10:\n", + " return True\n", + " else:\n", + " return False\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "727aa4f7", + "metadata": { + "id": "727aa4f7", + "outputId": "f14079c8-36f5-4f37-b613-2033cfe4d456" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "product_price_lessthan10({'rice':5})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "16ac4f63", + "metadata": { + "id": "16ac4f63", + "outputId": "b968d229-14e7-4b05-c024-ab16f9eabd86" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['rice'], ['wheat'], ['salt']]\n" + ] + } + ], + "source": [ + "product_and_price_list=list(filter(product_price_lessthan10, [{'rice':5}, {'wheat':9}, {'sugar':12},{'salt':5}] ))\n", + "\n", + "product_list = []\n", + "for x in product_and_price_list:\n", + " product_list.append( list(x.keys()) )\n", + "\n", + "print(product_list)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33b8a9b1", + "metadata": { + "id": "33b8a9b1" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "f7317674", + "metadata": { + "id": "f7317674" + }, + "source": [ + "### Q9. Given a list of strings, use filter to find all the strings that start with the letter 'A'." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9ce16192", + "metadata": { + "id": "9ce16192" + }, + "outputs": [], + "source": [ + "def starts_with_A(string):\n", + " upper_string = convert_to_uppercase(string)\n", + " if upper_string.startswith('A'):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "660e12ae", + "metadata": { + "id": "660e12ae", + "outputId": "e4f1420f-db32-4ce3-c794-39a22d0a35b8" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "starts_with_A('aritra')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2470859", + "metadata": { + "id": "c2470859", + "outputId": "740e6790-c647-49a7-b294-44338670a0ab" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['amal', 'abul', 'animesh']" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(starts_with_A, ['amal', 'kamal', 'abul', 'ritwik', 'animesh']))" + ] + }, + { + "cell_type": "markdown", + "id": "f9345b2f", + "metadata": { + "id": "f9345b2f" + }, + "source": [ + "### Q10. Given a list of numbers, use map to calculate the cubes of the even numbers within the list of numbers." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "360dfe37", + "metadata": { + "id": "360dfe37" + }, + "outputs": [], + "source": [ + "def is_even(n):\n", + " if n%2 == 0:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e33c6ad4", + "metadata": { + "id": "e33c6ad4" + }, + "outputs": [], + "source": [ + "def cubes_of_even_number(n):\n", + " if is_even(n)==True:\n", + " return n*n*n\n", + " else:\n", + " return 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76913dc6", + "metadata": { + "id": "76913dc6", + "outputId": "c12d8b14-2171-4a4d-9bb0-ac4960d1dc35" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[-64, 0, 8, 0, 64, 0, 216]" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(cubes_of_even_number, [-4,1,2,3,4,5,6]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "991e13b5", + "metadata": { + "id": "991e13b5" + }, + "outputs": [], + "source": [ + "# def test(n):\n", + "# if n%2 == 0:\n", + "# return n*n*n\n", + "# else:\n", + "# return False\n", + "\n", + "# list(map(test, [1,2,3,4,5,6]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75799665", + "metadata": { + "id": "75799665" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "9044f1ec", + "metadata": { + "id": "9044f1ec" + }, + "source": [ + "### Q11. Given a list of tuples (name, score), use map and filter to find the names of students who have scored above 80." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6baa4c8b", + "metadata": { + "id": "6baa4c8b" + }, + "outputs": [], + "source": [ + "def scored_above_80(name_and_score):\n", + " if name_and_score[1]>80:\n", + "# return name_and_score[0]\n", + " return True\n", + " else:\n", + " return False\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6af4cbca", + "metadata": { + "id": "6af4cbca", + "outputId": "ae49f4e7-8c27-462a-84dc-497b691f3dd2" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "scored_above_80(('Ritwik', 95))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5b703d44", + "metadata": { + "id": "5b703d44", + "outputId": "67c74a0e-802c-477c-a9d5-d59a74c8fa85" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rit\n", + "Mon\n" + ] + } + ], + "source": [ + "name_score_list = list(filter(scored_above_80, [('Rit', 95), ('Mon', 90), ('Ratul', 75)] ) )\n", + "for x in name_score_list:\n", + " print(x[0])" + ] + }, + { + "cell_type": "markdown", + "id": "d6789fde", + "metadata": { + "id": "d6789fde" + }, + "source": [ + "### Q12. Given a list of strings, use map to reverse each string." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64fccb5b", + "metadata": { + "id": "64fccb5b" + }, + "outputs": [], + "source": [ + "def reverse_string(string):\n", + " rev_string = string[::-1]\n", + " return rev_string" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4b859f6f", + "metadata": { + "id": "4b859f6f", + "outputId": "08ce304b-ba52-40fc-a0fa-ed9535d316c8" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'lamak'" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "reverse_string('kamal')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e651af29", + "metadata": { + "id": "e651af29", + "outputId": "89924dfc-0261-4ff3-e931-b38865665588" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['ahas', 'hsekar', 'ananab', 'luniom', 'nomel']" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(reverse_string, ['saha', 'rakesh', 'banana', 'moinul', 'lemon']))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74115657", + "metadata": { + "id": "74115657" + }, + "outputs": [], + "source": [ + "# string = 'kamal'\n", + "# string_characters=[]\n", + "# n = len(string)\n", + "\n", + "# for i in range(n):\n", + "# string_characters.append(string[i])\n", + "\n", + "# print(string_characters)\n", + "\n", + "# rev_string_characters = []\n", + "# for j in range(1,n+1):\n", + "# rev_string_characters.append(string_characters[-j])\n", + "\n", + "# print(rev_string_characters)\n", + "\n", + "# rev_string=''\n", + "# for i in range(n):\n", + "# rev_string += rev_string_characters[i]\n", + "\n", + "# print(rev_string)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e254687", + "metadata": { + "id": "3e254687" + }, + "outputs": [], + "source": [ + "# string= 'kamal'\n", + "# print(string)\n", + "# string_characters=[]\n", + "\n", + "# n = len(string)\n", + "# for i in range(n):\n", + "# string_characters.append(string[i])\n", + "\n", + "# print(string_characters)\n", + "# string_characters.reverse()\n", + "# print(string_characters)\n", + "\n", + "# rev_string=''\n", + "# for i in range(n):\n", + "# rev_string += string_characters[i]\n", + "\n", + "# print(rev_string)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "179390fc", + "metadata": { + "id": "179390fc" + }, + "outputs": [], + "source": [ + "# s='kamal'\n", + "# p=s[::-1]\n", + "# print(p)\n", + "# print(s)" + ] + }, + { + "cell_type": "markdown", + "id": "9362d834", + "metadata": { + "id": "9362d834" + }, + "source": [ + "### Q13. Given a list of integers, use filter to find all the numbers that are divisible by 7 and 9.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66cf00fd", + "metadata": { + "id": "66cf00fd" + }, + "outputs": [], + "source": [ + "def divisible_by_7and9(n):\n", + " if (n%7 == 0) and (n%9 == 0):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64bac34f", + "metadata": { + "id": "64bac34f", + "outputId": "7a40805a-0f13-4805-d09a-2025c402881a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible_by_7and9(63)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e0089596", + "metadata": { + "id": "e0089596", + "outputId": "18174a44-a0e7-4487-b872-ec55e0b25034" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[63, 126, -63, 0]" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(divisible_by_7and9, [7,9,63,126,133,-63,0,5] ))" + ] + }, + { + "cell_type": "markdown", + "id": "6bff530b", + "metadata": { + "id": "6bff530b" + }, + "source": [ + "### Q14. Given a list of dictionaries representing books (each dictionary contains keys 'title' and 'author'), use map to extract the titles of the books." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18fc8d2c", + "metadata": { + "id": "18fc8d2c" + }, + "outputs": [], + "source": [ + "def extract_title(dictionary_title_and_author):\n", + " title = tuple(dictionary_title_and_author.keys())\n", + " return title" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "580563ad", + "metadata": { + "id": "580563ad", + "outputId": "bc76d281-4cdc-4dfb-b406-f5cd370de76d" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "('Dynamical system in ecology',)" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "extract_title({'Dynamical system in ecology':'Ritwik Saha'})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e6d25c59", + "metadata": { + "id": "e6d25c59", + "outputId": "b2c0f659-f0b5-40c3-95ea-afce77430c74" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[('Book 1',), ('Book 2',), ('Book 3',)]" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(map(extract_title, [{'Book 1':'Ritwik'}, {'Book 2':'Manu'}, {'Book 3':'Rajesh'} ]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "82ba39f3", + "metadata": { + "id": "82ba39f3" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "d119f03b", + "metadata": { + "id": "d119f03b" + }, + "source": [ + "### Q15.Given a list of strings, use filter to find all the strings that contain the letter 'e'." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c409bf7f", + "metadata": { + "id": "c409bf7f" + }, + "outputs": [], + "source": [ + "def contain_e(string):\n", + " string = string.lower()\n", + " if 'e' in string:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48448817", + "metadata": { + "id": "48448817", + "outputId": "1bf44b15-3dfa-45ab-ec87-6588f6d0cdf6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "contain_e('ElEphant')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1dbb7281", + "metadata": { + "id": "1dbb7281", + "outputId": "4435a291-0955-4097-a25f-93786461af0f" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['ELEPHANT', 'Eligible', 'emi']" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(filter(contain_e, ['ELEPHANT', 'Eligible', 'Account', 'emi']))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7bd10549", + "metadata": { + "id": "7bd10549" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "8cadef79", + "metadata": { + "id": "8cadef79" + }, + "source": [ + "### Q16. Given a list of tuples (name, salary), use filter to find all the employees whose salary is above the average salary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf4e1158", + "metadata": { + "id": "bf4e1158" + }, + "outputs": [], + "source": [ + "def find_salary(tuple_of_name_salary):\n", + " return tuple_of_name_salary[1]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4273066", + "metadata": { + "id": "c4273066" + }, + "outputs": [], + "source": [ + "def avg_salary(salary_list):\n", + " sum1 = 0\n", + " n = len(salary_list)\n", + " for x in salary_list:\n", + " sum1 += x\n", + "\n", + " average_salary = sum1/n\n", + " return average_salary" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff3ee418", + "metadata": { + "id": "ff3ee418" + }, + "outputs": [], + "source": [ + "def employees_above_avgsalary(list_of_tuple_name_salary):\n", + " salary_list = list(map(find_salary, list_of_tuple_name_salary ))\n", + " average_salary = avg_salary(salary_list)\n", + "\n", + "# print(salary_list)\n", + " print(f'average salary is: {average_salary}')\n", + "# print(len(list_of_tuple_name_salary))\n", + "\n", + " ans=[]\n", + "\n", + " for i in range(len(list_of_tuple_name_salary)):\n", + " if list_of_tuple_name_salary[i][1] > average_salary:\n", + " ans.append(list_of_tuple_name_salary[i][0])\n", + "\n", + " return ans" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bf088aff", + "metadata": { + "id": "bf088aff", + "outputId": "01fe7cc3-13e7-4f25-d6c3-ec71a02c297a" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average salary is: 88.75\n" + ] + }, + { + "data": { + "text/plain": [ + "['ritwik', 'moinul', 'kanu']" + ] + }, + "execution_count": 172, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "employees_above_avgsalary([('ritwik',100),('moinul',90),('ram',60),('kanu',105)])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7622a2d8", + "metadata": { + "id": "7622a2d8" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9c4fb63a", + "metadata": { + "id": "9c4fb63a" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d5b1997", + "metadata": { + "id": "7d5b1997" + }, + "outputs": [], + "source": [ + "# def average_salary(name_and_salary):\n", + "# sum1=0\n", + "# salary=[]\n", + "# for x in name_and_salary:\n", + "# salary.append(x[1])\n", + "\n", + "# n=len(salary)\n", + "\n", + "# for y in salary:\n", + "# sum1=sum1+y\n", + "\n", + "# average_salary = sum1/n\n", + "\n", + "# # return average_salary\n", + "\n", + "# list1=[]\n", + "# for x in name_and_salary:\n", + "# if x[1] > average_salary:\n", + "# list1.append(True)\n", + "# else:\n", + "# list1.append(False)\n", + "\n", + "# return list1\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "9494495a", + "metadata": { + "id": "9494495a" + }, + "source": [ + "## USE FILTER .........." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "38b49fcd", + "metadata": { + "id": "38b49fcd" + }, + "outputs": [], + "source": [ + "# name_and_salary=[('Ritwik',100),('Moinul',150),('Mousumi',110),('Shyamal',70)]\n", + "# avg_salary = average_salary(name_and_salary)\n", + "# print(f'average salary of the employees is: {avg_salary}')\n", + "# list2=[]\n", + "# for x in name_and_salary:\n", + "# if x[1]>avg_salary:\n", + "# list2.append(x[0])\n", + "\n", + "# print(list2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12e7f971", + "metadata": { + "id": "12e7f971" + }, + "outputs": [], + "source": [ + "# name_and_salary=[('Ritwik',100),('Moinul',150),('Mousumi',110),('Shyamal',70)]\n", + "# list2=average_salary(name_and_salary)\n", + "# print(list2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c2d35d9", + "metadata": { + "id": "6c2d35d9" + }, + "outputs": [], + "source": [ + "# name_and_salary=[('Ritwik',100),('Moinul',150),('Mousumi',110),('Shyamal',70)]\n", + "# sum1=0\n", + "# for i in range(len(name_and_salary)):\n", + "# sum1+=name_and_salary[i][1]\n", + "\n", + "# average_salary = sum1/len(name_and_salary)\n", + "# print(sum1)\n", + "# print(average_salary)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53b29317", + "metadata": { + "id": "53b29317" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5bf4d764", + "metadata": { + "id": "5bf4d764" + }, + "outputs": [], + "source": [ + "# name_and_salary = [('Ritwik',100),('Moinul',150),('Mousumi',110),('Shyamal',70)]\n", + "# sum=0\n", + "# salary=[]\n", + "# for x in name_and_salary:\n", + "# salary.append(x[1])\n", + "\n", + "# n=len(salary)\n", + "# print(n)\n", + "# for y in salary:\n", + "# sum=sum+y\n", + "\n", + "# average_salary=sum/n\n", + "# print(sum)\n", + "# print(average_salary)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "48cc99c0", + "metadata": { + "id": "48cc99c0" + }, + "outputs": [], + "source": [] + } + ], + "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.10.9" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file