diff --git a/Python-ifed.ipynb b/Python-ifed.ipynb index ee9444d..0d58e79 100644 --- a/Python-ifed.ipynb +++ b/Python-ifed.ipynb @@ -62,15 +62,15 @@ "### Describe what each of the following means\n", "\n", "#### Production\n", - "(Insert answer here)\n", + "Production code runs on production servers so that it is the best version known to the public. It meets expectations in reliability, efficiency, etc.\n", "#### Clean \n", - "(Insert answer here)\n", + "Clean code is a concise, simple and readable code\n", "#### Modular\n", - "(Insert answer here)\n", + "Code that is logically broken up into functions and modules so that is more organized, efficient, and reusable.\n", "#### Module\n", - "(Insert answer here)\n", + "A file of code that can be imported to other files, so that the code can be reused\n", "#### Refactoring code\n", - "(Insert answer here)" + "improving the existing code without adding new functionality and the external features remain the same." ] }, { @@ -93,9 +93,214 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Seasoncitydateteam 1team 2toss winnertoss decisionresultdl appliedwinnerwin by runswin by wicketsplayer of matchvenueumpire 1umpire 2
id
1IPL-2017Hyderabad05-04-2017Sunrisers HyderabadRoyal Challengers BangaloreRoyal Challengers Bangalorefieldnormal0Sunrisers Hyderabad350Yuvraj SinghRajiv Gandhi International Stadium, UppalAY DandekarNJ Llong
2IPL-2017Pune06-04-2017Mumbai IndiansRising Pune SupergiantRising Pune Supergiantfieldnormal0Rising Pune Supergiant07SPD SmithMaharashtra Cricket Association StadiumA Nand KishoreS Ravi
3IPL-2017Rajkot07-04-2017Gujarat LionsKolkata Knight RidersKolkata Knight Ridersfieldnormal0Kolkata Knight Riders010CA LynnSaurashtra Cricket Association StadiumNitin MenonCK Nandan
4IPL-2017Indore08-04-2017Rising Pune SupergiantKings XI PunjabKings XI Punjabfieldnormal0Kings XI Punjab06GJ MaxwellHolkar Cricket StadiumAK ChaudharyC Shamshuddin
5IPL-2017Bangalore08-04-2017Royal Challengers BangaloreDelhi DaredevilsRoyal Challengers Bangalorebatnormal0Royal Challengers Bangalore150KM JadhavM Chinnaswamy StadiumNaNNaN
\n", + "
" + ], + "text/plain": [ + " Season city date team 1 \\\n", + "id \n", + "1 IPL-2017 Hyderabad 05-04-2017 Sunrisers Hyderabad \n", + "2 IPL-2017 Pune 06-04-2017 Mumbai Indians \n", + "3 IPL-2017 Rajkot 07-04-2017 Gujarat Lions \n", + "4 IPL-2017 Indore 08-04-2017 Rising Pune Supergiant \n", + "5 IPL-2017 Bangalore 08-04-2017 Royal Challengers Bangalore \n", + "\n", + " team 2 toss winner toss decision \\\n", + "id \n", + "1 Royal Challengers Bangalore Royal Challengers Bangalore field \n", + "2 Rising Pune Supergiant Rising Pune Supergiant field \n", + "3 Kolkata Knight Riders Kolkata Knight Riders field \n", + "4 Kings XI Punjab Kings XI Punjab field \n", + "5 Delhi Daredevils Royal Challengers Bangalore bat \n", + "\n", + " result dl applied winner win by runs \\\n", + "id \n", + "1 normal 0 Sunrisers Hyderabad 35 \n", + "2 normal 0 Rising Pune Supergiant 0 \n", + "3 normal 0 Kolkata Knight Riders 0 \n", + "4 normal 0 Kings XI Punjab 0 \n", + "5 normal 0 Royal Challengers Bangalore 15 \n", + "\n", + " win by wickets player of match venue \\\n", + "id \n", + "1 0 Yuvraj Singh Rajiv Gandhi International Stadium, Uppal \n", + "2 7 SPD Smith Maharashtra Cricket Association Stadium \n", + "3 10 CA Lynn Saurashtra Cricket Association Stadium \n", + "4 6 GJ Maxwell Holkar Cricket Stadium \n", + "5 0 KM Jadhav M Chinnaswamy Stadium \n", + "\n", + " umpire 1 umpire 2 \n", + "id \n", + "1 AY Dandekar NJ Llong \n", + "2 A Nand Kishore S Ravi \n", + "3 Nitin Menon CK Nandan \n", + "4 AK Chaudhary C Shamshuddin \n", + "5 NaN NaN " + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import pandas as pd\n", "df = pd.read_csv('matches.csv', sep=',')\n", @@ -113,9 +318,214 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Seasoncitydateteam_1team_2toss_winnertoss decisionresultdl_appliedwinnerwin_by_runswin_by_wicketsplayer_of_matchvenueumpire_1umpire_2
id
1IPL-2017Hyderabad05-04-2017Sunrisers HyderabadRoyal Challengers BangaloreRoyal Challengers Bangalorefieldnormal0Sunrisers Hyderabad350Yuvraj SinghRajiv Gandhi International Stadium, UppalAY DandekarNJ Llong
2IPL-2017Pune06-04-2017Mumbai IndiansRising Pune SupergiantRising Pune Supergiantfieldnormal0Rising Pune Supergiant07SPD SmithMaharashtra Cricket Association StadiumA Nand KishoreS Ravi
3IPL-2017Rajkot07-04-2017Gujarat LionsKolkata Knight RidersKolkata Knight Ridersfieldnormal0Kolkata Knight Riders010CA LynnSaurashtra Cricket Association StadiumNitin MenonCK Nandan
4IPL-2017Indore08-04-2017Rising Pune SupergiantKings XI PunjabKings XI Punjabfieldnormal0Kings XI Punjab06GJ MaxwellHolkar Cricket StadiumAK ChaudharyC Shamshuddin
5IPL-2017Bangalore08-04-2017Royal Challengers BangaloreDelhi DaredevilsRoyal Challengers Bangalorebatnormal0Royal Challengers Bangalore150KM JadhavM Chinnaswamy StadiumNaNNaN
\n", + "
" + ], + "text/plain": [ + " Season city date team_1 \\\n", + "id \n", + "1 IPL-2017 Hyderabad 05-04-2017 Sunrisers Hyderabad \n", + "2 IPL-2017 Pune 06-04-2017 Mumbai Indians \n", + "3 IPL-2017 Rajkot 07-04-2017 Gujarat Lions \n", + "4 IPL-2017 Indore 08-04-2017 Rising Pune Supergiant \n", + "5 IPL-2017 Bangalore 08-04-2017 Royal Challengers Bangalore \n", + "\n", + " team_2 toss_winner toss decision \\\n", + "id \n", + "1 Royal Challengers Bangalore Royal Challengers Bangalore field \n", + "2 Rising Pune Supergiant Rising Pune Supergiant field \n", + "3 Kolkata Knight Riders Kolkata Knight Riders field \n", + "4 Kings XI Punjab Kings XI Punjab field \n", + "5 Delhi Daredevils Royal Challengers Bangalore bat \n", + "\n", + " result dl_applied winner win_by_runs \\\n", + "id \n", + "1 normal 0 Sunrisers Hyderabad 35 \n", + "2 normal 0 Rising Pune Supergiant 0 \n", + "3 normal 0 Kolkata Knight Riders 0 \n", + "4 normal 0 Kings XI Punjab 0 \n", + "5 normal 0 Royal Challengers Bangalore 15 \n", + "\n", + " win_by_wickets player_of_match venue \\\n", + "id \n", + "1 0 Yuvraj Singh Rajiv Gandhi International Stadium, Uppal \n", + "2 7 SPD Smith Maharashtra Cricket Association Stadium \n", + "3 10 CA Lynn Saurashtra Cricket Association Stadium \n", + "4 6 GJ Maxwell Holkar Cricket Stadium \n", + "5 0 KM Jadhav M Chinnaswamy Stadium \n", + "\n", + " umpire_1 umpire_2 \n", + "id \n", + "1 AY Dandekar NJ Llong \n", + "2 A Nand Kishore S Ravi \n", + "3 Nitin Menon CK Nandan \n", + "4 AK Chaudhary C Shamshuddin \n", + "5 NaN NaN " + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "new_df = df.rename(columns={'team 1': 'team_1',\n", " 'team 2': 'team_2',\n", @@ -139,9 +549,214 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Seasoncitydateteam_1team 2toss_winnertoss_decisionresultdl_appliedwinnerwin_by_runswin by wicketsplayer of matchvenueumpire 1umpire 2
id
1IPL-2017Hyderabad05-04-2017Sunrisers HyderabadRoyal Challengers BangaloreRoyal Challengers Bangalorefieldnormal0Sunrisers Hyderabad350Yuvraj SinghRajiv Gandhi International Stadium, UppalAY DandekarNJ Llong
2IPL-2017Pune06-04-2017Mumbai IndiansRising Pune SupergiantRising Pune Supergiantfieldnormal0Rising Pune Supergiant07SPD SmithMaharashtra Cricket Association StadiumA Nand KishoreS Ravi
3IPL-2017Rajkot07-04-2017Gujarat LionsKolkata Knight RidersKolkata Knight Ridersfieldnormal0Kolkata Knight Riders010CA LynnSaurashtra Cricket Association StadiumNitin MenonCK Nandan
4IPL-2017Indore08-04-2017Rising Pune SupergiantKings XI PunjabKings XI Punjabfieldnormal0Kings XI Punjab06GJ MaxwellHolkar Cricket StadiumAK ChaudharyC Shamshuddin
5IPL-2017Bangalore08-04-2017Royal Challengers BangaloreDelhi DaredevilsRoyal Challengers Bangalorebatnormal0Royal Challengers Bangalore150KM JadhavM Chinnaswamy StadiumNaNNaN
\n", + "
" + ], + "text/plain": [ + " Season city date team_1 \\\n", + "id \n", + "1 IPL-2017 Hyderabad 05-04-2017 Sunrisers Hyderabad \n", + "2 IPL-2017 Pune 06-04-2017 Mumbai Indians \n", + "3 IPL-2017 Rajkot 07-04-2017 Gujarat Lions \n", + "4 IPL-2017 Indore 08-04-2017 Rising Pune Supergiant \n", + "5 IPL-2017 Bangalore 08-04-2017 Royal Challengers Bangalore \n", + "\n", + " team 2 toss_winner toss_decision \\\n", + "id \n", + "1 Royal Challengers Bangalore Royal Challengers Bangalore field \n", + "2 Rising Pune Supergiant Rising Pune Supergiant field \n", + "3 Kolkata Knight Riders Kolkata Knight Riders field \n", + "4 Kings XI Punjab Kings XI Punjab field \n", + "5 Delhi Daredevils Royal Challengers Bangalore bat \n", + "\n", + " result dl_applied winner win_by_runs \\\n", + "id \n", + "1 normal 0 Sunrisers Hyderabad 35 \n", + "2 normal 0 Rising Pune Supergiant 0 \n", + "3 normal 0 Kolkata Knight Riders 0 \n", + "4 normal 0 Kings XI Punjab 0 \n", + "5 normal 0 Royal Challengers Bangalore 15 \n", + "\n", + " win by wickets player of match venue \\\n", + "id \n", + "1 0 Yuvraj Singh Rajiv Gandhi International Stadium, Uppal \n", + "2 7 SPD Smith Maharashtra Cricket Association Stadium \n", + "3 10 CA Lynn Saurashtra Cricket Association Stadium \n", + "4 6 GJ Maxwell Holkar Cricket Stadium \n", + "5 0 KM Jadhav M Chinnaswamy Stadium \n", + "\n", + " umpire 1 umpire 2 \n", + "id \n", + "1 AY Dandekar NJ Llong \n", + "2 A Nand Kishore S Ravi \n", + "3 Nitin Menon CK Nandan \n", + "4 AK Chaudhary C Shamshuddin \n", + "5 NaN NaN " + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "labels = list(df.columns)\n", "labels[0] = labels[0].replace(' ', '_')\n", @@ -167,11 +782,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['Season', 'city', 'date', 'team 1', 'team 2', 'toss winner',\n", + " 'toss decision', 'result', 'dl applied', 'winner', 'win by runs',\n", + " 'win by wickets', 'player of match', 'venue', 'umpire 1', 'umpire 2'],\n", + " dtype='object')\n", + "Index(['Season', 'city', 'date', 'team_1', 'team 2', 'toss_winner',\n", + " 'toss_decision', 'result', 'dl_applied', 'winner', 'win_by_runs',\n", + " 'win by wickets', 'player of match', 'venue', 'umpire 1', 'umpire 2'],\n", + " dtype='object')\n" + ] + } + ], "source": [ - "# Insert your solution here" + "print(df.columns)\n", + "df.columns =[list(df.columns)[i].replace(' ', '_') if (i!=4 and i<11) else list(df.columns)[i] for i in range(16)]\n", + "#df.columns = labels + list(df.columns)[10:]\n", + "print(df.columns)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 4, 5]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = [1,2,3,4,5]\n", + "a = [i for i in a if i!=2]\n", + "a" ] }, { @@ -208,7 +863,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -219,7 +874,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -233,9 +888,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 8.536158084869385 seconds\n" + ] + } + ], "source": [ "start = time.time()\n", "verified_elements = []\n", @@ -243,7 +907,7 @@ "for element in subset_elements:\n", " if element in all_elements:\n", " verified_elements.append(element)\n", - "\n", + " \n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" ] @@ -257,11 +921,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.025506258010864258 seconds\n" + ] + } + ], "source": [ - "# Insert answer here\n", + "start = time.time()\n", + "verfied_elements = np.intersect1d(subset_elements,all_elements) \n", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -276,12 +950,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.007076740264892578 seconds\n" + ] + } + ], "source": [ + "start = time.time()\n", "# Insert answer here\n", + "subset_elements = set(subset_elements)\n", + "all_elements = set(all_elements)\n", "\n", + "verified_elements = subset_elements.intersection(all_elements)\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" ] @@ -330,9 +1017,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 93, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m============================= test session starts ==============================\u001b[0m\r\n", + "platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\r\n", + "rootdir: /home/lakshmi/my-Python-ifed-challenge\r\n", + "plugins: anyio-2.0.2\r\n", + "\u001b[1mcollecting ... \u001b[0m\u001b[1m\r", + "collected 4 items \u001b[0m\r\n", + "\r\n", + "test_nearest_square.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m [100%]\u001b[0m\r\n", + "\r\n", + "\u001b[32m============================== \u001b[32m\u001b[1m4 passed\u001b[0m\u001b[32m in 0.01s\u001b[0m\u001b[32m ===============================\u001b[0m\r\n" + ] + } + ], "source": [ "! pytest test_nearest_square.py" ] @@ -386,11 +1090,28 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 90, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "eevee\n", + "normal\n" + ] + } + ], "source": [ - "# Insert your code here" + "import requests, json\n", + "\n", + "response = requests.get('https://pokeapi.co/api/v2/pokemon/eevee')\n", + "\n", + "name = response.json()['species']['name']\n", + "print(name)\n", + "\n", + "poke_type = response.json()['types'][0]['type']['name']\n", + "print(poke_type)" ] }, { @@ -404,11 +1125,37 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 91, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "fighting\n" + ] + } + ], "source": [ - "# Insert your code here" + "response = requests.get('https://pokeapi.co/api/v2/pokemon/eevee')\n", + "\n", + "'''\n", + "##Add a for loop here - for more than 1 type\n", + "'''\n", + "url = response.json()['types'][0]['type']['url']\n", + "\n", + "response= requests.get(url)\n", + "dam_rel = response.json()['damage_relations']\n", + "\n", + "#extracting every key with substring damage_from, except no_damage_from\n", + "damage_list = [value for key, value in dam_rel.items() if ('damage_from' in key and 'no' not in key)]\n", + "# damage_from is a list of lists\n", + "\n", + "for damage_from in damage_list:\n", + " #iterating through lists\n", + " for damage in damage_from:\n", + " #damage - dictionary\n", + " print(damage['name'])\n" ] }, { @@ -420,11 +1167,37 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 87, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Double damage from:\n", + "fighting\n", + "https://pokeapi.co/api/v2/type/2/\n", + "mankey\n", + "primeape\n", + "poliwrath\n", + "machop\n", + "machoke\n" + ] + } + ], "source": [ - "# Insert your code here" + "print(\"Double damage from:\")\n", + "double_dam_url = []\n", + "for dam in range(len(dam_rel['double_damage_from'])):\n", + " double_dam_url.append(dam_rel['double_damage_from'][dam]['url'])\n", + " print(dam_rel['double_damage_from'][dam]['name'])\n", + "print((double_dam_url[0]))\n", + "\n", + "for dam_url in double_dam_url:\n", + " response = requests.get(dam_url)\n", + " pokemon = response.json()['pokemon']\n", + " for ind in range(5):\n", + " print(pokemon[ind]['pokemon']['name'])" ] }, { @@ -438,7 +1211,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 92, "metadata": {}, "outputs": [ { @@ -446,18 +1219,19 @@ "output_type": "stream", "text": [ "\u001b[1m============================= test session starts ==============================\u001b[0m\n", - "platform darwin -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\n", - "rootdir: /Users/abhijitramesh/development/Python-ifed\n", + "platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\n", + "rootdir: /home/lakshmi/my-Python-ifed-challenge\n", + "plugins: anyio-2.0.2\n", "collected 4 items \u001b[0m\n", "\n", "test_abilities.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m [100%]\u001b[0m\n", "\n", - "\u001b[32m============================== \u001b[32m\u001b[1m4 passed\u001b[0m\u001b[32m in 2.75s\u001b[0m\u001b[32m ===============================\u001b[0m\n" + "\u001b[32m============================== \u001b[32m\u001b[1m4 passed\u001b[0m\u001b[32m in 1.05s\u001b[0m\u001b[32m ===============================\u001b[0m\n" ] } ], "source": [ - "!pytest test_abilities.py" + "! pytest test_abilities.py" ] }, { @@ -515,9 +1289,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} diff --git a/__pycache__/ability.cpython-38.pyc b/__pycache__/ability.cpython-38.pyc new file mode 100644 index 0000000..8c860a9 Binary files /dev/null and b/__pycache__/ability.cpython-38.pyc differ diff --git a/__pycache__/nearest_square.cpython-38.pyc b/__pycache__/nearest_square.cpython-38.pyc new file mode 100644 index 0000000..95b7b74 Binary files /dev/null and b/__pycache__/nearest_square.cpython-38.pyc differ diff --git a/__pycache__/pokedex.cpython-38.pyc b/__pycache__/pokedex.cpython-38.pyc new file mode 100644 index 0000000..b881a2a Binary files /dev/null and b/__pycache__/pokedex.cpython-38.pyc differ diff --git a/__pycache__/test_abilities.cpython-38-pytest-6.2.1.pyc b/__pycache__/test_abilities.cpython-38-pytest-6.2.1.pyc new file mode 100644 index 0000000..0c5ed66 Binary files /dev/null and b/__pycache__/test_abilities.cpython-38-pytest-6.2.1.pyc differ diff --git a/__pycache__/test_nearest_square.cpython-38-pytest-6.2.1.pyc b/__pycache__/test_nearest_square.cpython-38-pytest-6.2.1.pyc new file mode 100644 index 0000000..21ad73c Binary files /dev/null and b/__pycache__/test_nearest_square.cpython-38-pytest-6.2.1.pyc differ diff --git a/ability.py b/ability.py new file mode 100644 index 0000000..b3585da --- /dev/null +++ b/ability.py @@ -0,0 +1,11 @@ +import requests + +def ability(pokemon): + response = requests.get('https://pokeapi.co/api/v2/pokemon/'+pokemon) + abilities = response.json()['abilities'] + + ablility_list = [] + for ind in range(len(abilities)): + ablility_list.append(abilities[ind]['ability']['name']) + return ablility_list + diff --git a/nearest_square.py b/nearest_square.py new file mode 100644 index 0000000..9a1d779 --- /dev/null +++ b/nearest_square.py @@ -0,0 +1,8 @@ +import math + +def nearest_square(num): + if num<0: + return 0 + return(math.floor(math.sqrt(num))**2) + + diff --git a/pokedex.py b/pokedex.py new file mode 100644 index 0000000..af1b0b7 --- /dev/null +++ b/pokedex.py @@ -0,0 +1,91 @@ +import requests +import json + + +def get_type(url): + ''' + Gets the type of Pokemon + + PARAMETERS: + ------------- + url: string + + RETURNS: + ------------- + type of Pokemon : string + + ''' + + response = requests.get(url) + + name = response.json()['species']['name'] + print(name) + + poke_types = [] + poke_type_list = response.json()['types'] + for i in range(len(poke_type_list)): + poke_types.append(poke_type_list[i]['type']['name']) + print(poke_types) + + return ", ".join(poke_types) + + +def damage_from(url): + ''' + Gets the type of damage + + PARAMETERS: + ------------- + url: string + + RETURNS: + ------------- + type of damage : list + + ''' + response = requests.get(url) + + list_to_return = [] + + types = response.json()['types'] + for i in range(len(types)): + url = types[i]['type']['url'] + + response = requests.get(url) + dam_rel = response.json()['damage_relations'] + + # extracting every key with substring damage_from, except no_damage_from + damage_list = [value for key, value in dam_rel.items() if ( + 'damage_from' in key and 'no' not in key)] + # damage_from is a list of lists + + for damage_from in damage_list: + # iterating through lists + for damage in damage_from: + #damage - dictionary + list_to_return.append(damage['name']) + print(damage['name']) + return list_to_return + + +def get5_attackers(url): + response = requests.get(url) + + print("Double damage from:") + double_dam_url = [] + dam_rel = response.json()['damage_relations'] + + for dam in range(len(dam_rel['double_damage_from'])): + double_dam_url.append(dam_rel['double_damage_from'][dam]['url']) + print(dam_rel['double_damage_from'][dam]['name']) + print((double_dam_url[0])) + + attackers = [] + for dam_url in double_dam_url: + response = requests.get(dam_url) + pokemon = response.json()['pokemon'] + for ind in range(5): + attackers.append(pokemon[ind]['pokemon']['name']) + print(pokemon[ind]['pokemon']['name']) + + return attackers diff --git a/pokedex_design.py b/pokedex_design.py new file mode 100644 index 0000000..48cc98d --- /dev/null +++ b/pokedex_design.py @@ -0,0 +1,39 @@ +import PySimpleGUI as sg +import pokedex as pd + +# Define the window's contents +layout = [[sg.Text("Name of the pokemon")], # Part 2 - The Layout + [sg.Input()], + [sg.Button('Ok')], + ] + +# Create the window +window = sg.Window('Pokedex', layout) +# Display and interact with the Window +event, values = window.read() + +pokemon = values[0].lower() +print('Pokemon: ', pokemon) + +url = 'https://pokeapi.co/api/v2/pokemon/'+pokemon + +poke_type = pd.get_type(url) +damage_from = pd.damage_from(url) +new_url = ['https://pokeapi.co/api/v2/pokemon/type/'+i for i in damage_from] +print(new_url) +poke_type_column = [ + [sg.Text("Type: "+poke_type.capitalize())], + [sg.Text("Damage from: "+"".join(damage_from).capitalize())], + #[sg.Text("Attackers: "+"".join(pd.get5_attackers(new_url[0])).capitalize())], + + [sg.Text(size=(40, 1), key="-TOUT-")], +] + +menu = [[sg.Column(poke_type_column)]] +window = sg.Window('Pokedex', menu) +event, values = window.read() + + + +# Finish up by removing from the screen +window.close() diff --git a/step_4.py b/step_4.py new file mode 100644 index 0000000..d9cf0a6 --- /dev/null +++ b/step_4.py @@ -0,0 +1,101 @@ +import time +import numpy as np + +def common_elems_num1(subset_elements, all_elements): + ''' + Gets the number of common elements using the set operation - intersection + PARAMETERS: + ------------- + subset_elements: list of strings, + all_elements : list of strings + + RETURNS: + ------------- + number of common elements and the duration for execution + + ''' + + # records the starting time to calculate the time taken for implementation + start = time.time() + + #converting the lists to set for implementing the intersection function + subset_elements = set(subset_elements) + all_elements = set(all_elements) + + #intersection - returns all the common elements in both the list as a new set + verified_elements = subset_elements.intersection(all_elements) + + # duration is calculated and printed + duration = time.time() - start + + return (len(verified_elements), duration) + +def common_elems_num2(subset_elements, all_elements): + ''' + Gets the number of common elements using the NumPy array operation - intersect1d + PARAMETERS: + ------------- + subset_elements: list of strings + all_elements : list of strings + + RETURNS: + ------------- + number of common elements and the duration for execution + + ''' + # records the starting time to calculate the time taken for implementation + start = time.time() + + # an array of all the common elements is stored in verified_elements + verfied_elements = np.intersect1d(subset_elements,all_elements) + + # recording the time taken + duration = time.time() - start + + return len(verfied_elements), duration + +def common_elems_num3(subset, all_elements): + ''' + Gets the number of common elements using the traditional for-loop + comparison method + PARAMETERS: + ------------- + subset_elements: list of strings + all_elements : list of strings + + RETURNS: + ------------- + number of common elements and the duration for execution + + ''' + + #the time when the execution begins + start = time.time() + #an empty list to store the common elements from both the lists + verified_elements = [] + + for element in subset: + # looping through subset + if element in all_elements: + # checking if 'element' is present in 'all_elements' and appending to verified_elements + verified_elements.append(element) + + #calculate duration + duration = time.time() - start + return len(verified_elements), duration + + +if __name__ == "__main__": + with open('subset_elemets.txt') as f: + subset_elements = f.read().split('\n') + + with open('all_elements.txt') as f: + all_elements = f.read().split('\n') + #lines in the files are split and stored into the lists + + print("Number of elements and time taken in s for three different approaches") + print("First approach: Using NumPy") + print(common_elems_num1(subset_elements, all_elements )) + print("\nSecond approach: Using Set") + print (common_elems_num2(subset_elements, all_elements )) + print("\nThird approach: Using traditional for loop and comparing") + print (common_elems_num3(subset_elements, all_elements )) \ No newline at end of file diff --git a/test_nearest_square.py b/test_nearest_square.py index c5d1e43..330d446 100644 --- a/test_nearest_square.py +++ b/test_nearest_square.py @@ -7,4 +7,5 @@ def test_nearest_square_n12(): def test_nearest_square_9(): assert(nearest_square(9)==9) def test_nearest_square_23(): - assert(nearest_square(23)==16) \ No newline at end of file + assert(nearest_square(23)==16) +