diff --git a/.ipynb_checkpoints/Python-ifed-checkpoint.ipynb b/.ipynb_checkpoints/Python-ifed-checkpoint.ipynb new file mode 100644 index 0000000..990bbe8 --- /dev/null +++ b/.ipynb_checkpoints/Python-ifed-checkpoint.ipynb @@ -0,0 +1,1257 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Python-ifed\n", + "\n", + "Welcome to the python-ifed notebook, This notebook is a walkthrough + task for you to get started with software development using python.\n", + "\n", + "After completing this notebook successfully you should be familiar with the following concepts:\n", + "* Use version control\n", + "* Writing clean and modular code\n", + "* Improve code efficiency\n", + "* Add effective documentation\n", + "* Testing \n", + "* Code reviews\n", + "\n", + "**This exercise depends on a lot of googling skills and is aimed at making you efficient in the same**." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 1 : Tables and stuff" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 0\n", + "\n", + "You might be already done with this step\n", + "\n", + "### Learn to version control using git\n", + "\n", + "1. Install git CLI tool\n", + "1. Configure git CLI\n", + "1. Create a GitHub account if you already have not \n", + "1. Clone the repo that contains this repository\n", + "1. Now you are in master branch\n", + "1. Create a new branch with your name as the branch name and continue\n", + "\n", + "### Reference materials\n", + "https://www.atlassian.com/git\n", + "\n", + "https://www.tutorialspoint.com/git/git_basic_concepts.htm" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 1\n", + "\n", + "## Clean and modular code\n", + "\n", + "Test out your googling skills and understand what writing clean and modular code means and answer the following questions by editing this cell in jupyter notebook.\n", + "\n", + "### Describe what each of the following means\n", + "\n", + "#### Production\n", + "Production means anything that you need to work reliably, and consistently.\n", + "#### Clean \n", + "Readable and easy to understand by everyone whether the reader is the author of the code or a new programmer.\n", + "#### Modular\n", + "Splitting big blocks of code into smaller, understandable blocks of code.\n", + "#### Module\n", + "A single block of code.\n", + "#### Refactoring code\n", + "The process of restructuring computer code without changing or adding to its external behavior and functionality." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you have finished writing the answers you can now commit these new changes with git using the commit message __step 1 completed__ ." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 2\n", + "\n", + "## Refactor: Cricket Match Analysis\n", + "\n", + "Here I would be providing you with a sample code, don't worry about the working much try to get an abstract idea and complete the task" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "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": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.read_csv('matches.csv', sep=',')\n", + "df.set_index('id',inplace=True)\n", + "df.drop('umpire3',axis=1,inplace=True)\n", + "df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is how our data looks like right now we need to write efficient code to replaces the spaces with an underscore, the janky way of doing this is" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "new_df = df.rename(columns={'team 1': 'team_1',\n", + " 'team 2': 'team_2',\n", + " 'toss winner': 'toss_winner',\n", + " 'dl applied': 'dl_applied',\n", + " 'win by runs': 'win_by_runs',\n", + " 'win by wickets': 'win_by_wickets',\n", + " 'player of match': 'player_of_match',\n", + " 'umpire 1':'umpire_1',\n", + " 'umpire 2':'umpire_2'\n", + " })\n", + "new_df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is like a hardcoded way of doing it slightly better way of doing this is " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "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": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "labels = list(df.columns)\n", + "labels[0] = labels[0].replace(' ', '_')\n", + "labels[1] = labels[1].replace(' ', '_')\n", + "labels[2] = labels[2].replace(' ', '_')\n", + "labels[3] = labels[3].replace(' ', '_')\n", + "labels[5] = labels[5].replace(' ', '_')\n", + "labels[6] = labels[6].replace(' ', '_')\n", + "labels[7] = labels[7].replace(' ', '_')\n", + "labels[8] = labels[8].replace(' ', '_')\n", + "labels[9] = labels[9].replace(' ', '_')\n", + "labels[10] = labels[10].replace(' ', '_')\n", + "df.columns = labels\n", + "df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is also a very redundant way of doing this try writing a better code to do the same. Limit yourselves to one or two lines of code." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "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": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "for labels in list(df.columns):\n", + " labels = labels.replace(' ', '_')\n", + "df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once you are done till here make a new commit with message __step 2 complete__." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 3\n", + "\n", + "## Optimizing Code\n", + "\n", + "### Efficient Code\n", + "\n", + "Knowing how to write code that runs efficiently is another essential skill in software development. Optimizing code to be more efficient can mean making it:\n", + "\n", + "* Execute faster\n", + "* Take up less space in memory/storage\n", + "\n", + "\n", + "Resources:\n", + "https://stackify.com/20-simple-python-performance-tuning-tips/\n", + "\n", + "https://pybit.es/faster-python.html\n", + "\n", + "https://towardsdatascience.com/one-simple-trick-for-speeding-up-your-python-code-with-numpy-1afc846db418\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "with open('subset_elemets.txt') as f:\n", + " subset_elements = f.read().split('\\n')\n", + " \n", + "with open('all_elements.txt') as f:\n", + " all_elements = f.read().split('\\n')\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 13.369797945022583 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "verified_elements = []\n", + "\n", + "for element in subset_elements:\n", + " if element in all_elements:\n", + " verified_elements.append(element)\n", + "\n", + "print(len(verified_elements))\n", + "print('Duration: {} seconds'.format(time.time() - start))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Use vector operations using NumPy to optimise the loop" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.046604156494140625 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "verified_elements = []\n", + "\n", + "for element in np.in1d(subset_elements,all_elements):\n", + " if element:\n", + " verified_elements.append(element)\n", + "\n", + "print(len(verified_elements))\n", + "print('Duration: {} seconds'.format(time.time() - start))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Use a python datastructure which has a method to peform this task faster" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.00812673568725586 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "\n", + "print(len(set(subset_elements).intersection(all_elements)))\n", + " \n", + "print('Duration: {} seconds'.format(time.time() - start))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 4\n", + "# Documentation\n", + "\n", + "Documentation is one of the important part of software development. Teach yourself about documentation in python and convert code in step 3 to functions and add relevant documentation for the same.\n", + "\n", + "#### Resources\n", + "https://www.python.org/dev/peps/pep-0257/\n", + "\n", + "https://numpydoc.readthedocs.io/en/latest/format.html\n", + "\n", + "https://www.datacamp.com/community/tutorials/documenting-python-code\n", + "\n", + "do not add this code to the notebook instead create a new python file called step_4.py and define the functions as well as a main to test the working of all the functions make sure to version this file as well on the commit message __step 4 completed__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 5\n", + "\n", + "# Testing\n", + "\n", + "Testing your code is essential before deployment. It helps you catch errors and faulty conclusions before they make any major impact. Today, employers are looking for data scientists with the skills to properly prepare their code for an industry setting, which includes testing their code.\n", + "\n", + "Learn about pytest and install it \n", + "\n", + "https://docs.pytest.org/en/stable/\n", + "\n", + "create a new file called nearest_square.py this function should return the nearest perfect square number which is less than or equal to the number.\n", + "\n", + "create another file called test_nearest_square.py to test the function with different test each for values 5,-12,9 and 23\n", + "\n", + "execute the line below to ensure it is working correctly\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", + "platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\n", + "rootdir: /home/arungovindm/Python-ifed-challenge\n", + "collected 4 items \u001b[0m\n", + "\n", + "test_nearest_square.py \u001b[32m.\u001b[0m\u001b[31mF\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[31m [100%]\u001b[0m\n", + "\n", + "=================================== FAILURES ===================================\n", + "\u001b[31m\u001b[1m___________________________ test_nearest_square_n12 ____________________________\u001b[0m\n", + "\n", + " \u001b[94mdef\u001b[39;49;00m \u001b[92mtest_nearest_square_n12\u001b[39;49;00m():\n", + "> \u001b[94massert\u001b[39;49;00m(nearest_square(-\u001b[94m12\u001b[39;49;00m)==\u001b[94m0\u001b[39;49;00m)\n", + "\u001b[1m\u001b[31mE assert 9 == 0\u001b[0m\n", + "\u001b[1m\u001b[31mE + where 9 = nearest_square(-12)\u001b[0m\n", + "\n", + "\u001b[1m\u001b[31mtest_nearest_square.py\u001b[0m:6: AssertionError\n", + "=========================== short test summary info ============================\n", + "FAILED test_nearest_square.py::test_nearest_square_n12 - assert 9 == 0\n", + "\u001b[31m========================= \u001b[31m\u001b[1m1 failed\u001b[0m, \u001b[32m3 passed\u001b[0m\u001b[31m in 0.12s\u001b[0m\u001b[31m ==========================\u001b[0m\n" + ] + } + ], + "source": [ + "! pytest test_nearest_square.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If you are done with this step make a new commit __step5 completed__" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Step 6 \n", + "# Code review\n", + "\n", + "Understand how code review works\n", + "\n", + "https://github.com/lyst/MakingLyst/tree/master/code-reviews\n", + "\n", + "https://www.kevinlondon.com/2015/05/05/code-review-best-practices.html\n", + "\n", + "_Leave it to us_ if you are done with this step go ahead and push this notebook as well as all the files to your GitHub and make a pull request to the repository that you cloned this task from so that we can review your progress till now, Please continue with the challenge" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Challenge 2 : Pokemon" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that you are familiar with writing efficient code let's use that practice and do some API fetching to answer some questions, Below are some questions below and you should use the [pokeapi](https://pokeapi.co/) to do some API fetching with python and answer the questions.\n", + "\n", + "**NOTE**\n", + "You should fill the cell with code that gives the answer and not write the answer directly, failing to do so will reduce your evaluation and proves that you did not bother reading this part." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Remember you were in a pokemon room (discord) what is the **type** of that pokemon" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "Type of golduck: water\n" + ] + } + ], + "source": [ + "import requests, json\n", + "req = requests.get('https://pokeapi.co/api/v2/pokemon/golduck/')\n", + "print(req.status_code)\n", + "pokedata = json.loads(req.text)\n", + "name = pokedata['name']\n", + "type = pokedata['types'][0]['type']['name']\n", + "print('Type of {}: {}'.format(name,type))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What **type** of pokemons does this **type** take damage from\n", + "\n", + "__hint__ the url field of previous response" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "grass\n", + "electric\n", + "steel\n", + "fire\n", + "water\n", + "ice\n" + ] + } + ], + "source": [ + "req = requests.get(pokedata['types'][0]['type']['url'])\n", + "print(req.status_code)\n", + "data = json.loads(req.text)\n", + "double_damage = data['damage_relations']['double_damage_from']\n", + "half_damage = data['damage_relations']['half_damage_from']\n", + "total_damage = double_damage+half_damage\n", + "for damage in total_damage:\n", + " print(damage['name'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For each of the **double damage from** type list 5 pokemons in that type" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bulbasaur\n", + "ivysaur\n", + "venusaur\n", + "oddish\n", + "gloom\n", + "pikachu\n", + "raichu\n", + "magnemite\n", + "magneton\n", + "voltorb\n" + ] + } + ], + "source": [ + "for elements in double_damage:\n", + " url = elements['url']\n", + " req = requests.get(url)\n", + " data = json.loads(req.text)\n", + " for i in range(5):\n", + " print(data['pokemon'][i]['pokemon']['name'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a function ability() inside a new file ability.py to return a list of the abilities of any given pokemon by doing an API query the function should accept a string parameter which is the name of the pokemon\n", + "\n", + "execute the line below to ensure everything is working properly" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", + "platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\n", + "rootdir: /home/arungovindm/Python-ifed-challenge\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 3.51s\u001b[0m\u001b[32m ===============================\u001b[0m\n" + ] + } + ], + "source": [ + "!pytest test_abilities.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Please version till this point saying with a message \"Completed challenge 2\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Extra Challenge for extra karma point" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If the above challenge was a cakewalk and you have a lot of time left to attempt this challenge to earn some karma points, this challenge is completely optional.\n", + "Let's create a pokedex with the function we created earlier on that is\n", + "\n", + "* What is the type of pokemon\n", + "* What type of pokemon gives double damages to the given pokemon\n", + "* List 5 pokemons which gives the given pokemon double damage\n", + "* Abilities of our pokemon\n", + "\n", + "Use [pysimplegui](https://pypi.org/project/PySimpleGUI) to create a simple pokedex which consumes these functions\n", + "\n", + "save the file as pokedex.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Version till this point with a message \"Completed Extra Challenge\"\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python-ifed.ipynb b/Python-ifed.ipynb index ee9444d..990bbe8 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 means anything that you need to work reliably, and consistently.\n", "#### Clean \n", - "(Insert answer here)\n", + "Readable and easy to understand by everyone whether the reader is the author of the code or a new programmer.\n", "#### Modular\n", - "(Insert answer here)\n", + "Splitting big blocks of code into smaller, understandable blocks of code.\n", "#### Module\n", - "(Insert answer here)\n", + "A single block of code.\n", "#### Refactoring code\n", - "(Insert answer here)" + "The process of restructuring computer code without changing or adding to its external behavior and functionality." ] }, { @@ -93,9 +93,214 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import pandas as pd\n", "df = pd.read_csv('matches.csv', sep=',')\n", @@ -139,9 +344,214 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "labels = list(df.columns)\n", "labels[0] = labels[0].replace(' ', '_')\n", @@ -167,11 +577,218 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "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": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Insert your solution here" + "for labels in list(df.columns):\n", + " labels = labels.replace(' ', '_')\n", + "df.head()" ] }, { @@ -208,7 +825,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -219,7 +836,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -233,9 +850,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 13.369797945022583 seconds\n" + ] + } + ], "source": [ "start = time.time()\n", "verified_elements = []\n", @@ -257,11 +883,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.046604156494140625 seconds\n" + ] + } + ], "source": [ - "# Insert answer here\n", + "start = time.time()\n", + "verified_elements = []\n", + "\n", + "for element in np.in1d(subset_elements,all_elements):\n", + " if element:\n", + " verified_elements.append(element)\n", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -276,13 +916,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.00812673568725586 seconds\n" + ] + } + ], "source": [ - "# Insert answer here\n", + "start = time.time()\n", "\n", - "print(len(verified_elements))\n", + "print(len(set(subset_elements).intersection(all_elements)))\n", + " \n", "print('Duration: {} seconds'.format(time.time() - start))" ] }, @@ -330,9 +980,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", + "platform linux -- Python 3.8.5, pytest-6.2.1, py-1.10.0, pluggy-0.13.1\n", + "rootdir: /home/arungovindm/Python-ifed-challenge\n", + "collected 4 items \u001b[0m\n", + "\n", + "test_nearest_square.py \u001b[32m.\u001b[0m\u001b[31mF\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[31m [100%]\u001b[0m\n", + "\n", + "=================================== FAILURES ===================================\n", + "\u001b[31m\u001b[1m___________________________ test_nearest_square_n12 ____________________________\u001b[0m\n", + "\n", + " \u001b[94mdef\u001b[39;49;00m \u001b[92mtest_nearest_square_n12\u001b[39;49;00m():\n", + "> \u001b[94massert\u001b[39;49;00m(nearest_square(-\u001b[94m12\u001b[39;49;00m)==\u001b[94m0\u001b[39;49;00m)\n", + "\u001b[1m\u001b[31mE assert 9 == 0\u001b[0m\n", + "\u001b[1m\u001b[31mE + where 9 = nearest_square(-12)\u001b[0m\n", + "\n", + "\u001b[1m\u001b[31mtest_nearest_square.py\u001b[0m:6: AssertionError\n", + "=========================== short test summary info ============================\n", + "FAILED test_nearest_square.py::test_nearest_square_n12 - assert 9 == 0\n", + "\u001b[31m========================= \u001b[31m\u001b[1m1 failed\u001b[0m, \u001b[32m3 passed\u001b[0m\u001b[31m in 0.12s\u001b[0m\u001b[31m ==========================\u001b[0m\n" + ] + } + ], "source": [ "! pytest test_nearest_square.py" ] @@ -386,11 +1062,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "Type of golduck: water\n" + ] + } + ], "source": [ - "# Insert your code here" + "import requests, json\n", + "req = requests.get('https://pokeapi.co/api/v2/pokemon/golduck/')\n", + "print(req.status_code)\n", + "pokedata = json.loads(req.text)\n", + "name = pokedata['name']\n", + "type = pokedata['types'][0]['type']['name']\n", + "print('Type of {}: {}'.format(name,type))" ] }, { @@ -404,11 +1095,32 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 20, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "grass\n", + "electric\n", + "steel\n", + "fire\n", + "water\n", + "ice\n" + ] + } + ], "source": [ - "# Insert your code here" + "req = requests.get(pokedata['types'][0]['type']['url'])\n", + "print(req.status_code)\n", + "data = json.loads(req.text)\n", + "double_damage = data['damage_relations']['double_damage_from']\n", + "half_damage = data['damage_relations']['half_damage_from']\n", + "total_damage = double_damage+half_damage\n", + "for damage in total_damage:\n", + " print(damage['name'])" ] }, { @@ -420,11 +1132,33 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bulbasaur\n", + "ivysaur\n", + "venusaur\n", + "oddish\n", + "gloom\n", + "pikachu\n", + "raichu\n", + "magnemite\n", + "magneton\n", + "voltorb\n" + ] + } + ], "source": [ - "# Insert your code here" + "for elements in double_damage:\n", + " url = elements['url']\n", + " req = requests.get(url)\n", + " data = json.loads(req.text)\n", + " for i in range(5):\n", + " print(data['pokemon'][i]['pokemon']['name'])" ] }, { @@ -438,7 +1172,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -446,13 +1180,13 @@ "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/arungovindm/Python-ifed-challenge\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 3.51s\u001b[0m\u001b[32m ===============================\u001b[0m\n" ] } ], @@ -515,9 +1249,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/ability.py b/ability.py new file mode 100644 index 0000000..220175f --- /dev/null +++ b/ability.py @@ -0,0 +1,8 @@ +def ability(name): + import requests,json + abilities=[] + req = requests.get('https://pokeapi.co/api/v2/pokemon/{}/'.format(name)) + pokedata = json.loads(req.text) + for ability in pokedata['abilities']: + abilities.append(ability['ability']['name']) + return abilities diff --git a/nearest_square.py b/nearest_square.py new file mode 100644 index 0000000..83d3501 --- /dev/null +++ b/nearest_square.py @@ -0,0 +1,6 @@ +def nearest_square(number): + absoluteNumber = abs(number) + sqrt = absoluteNumber**(1/2) + roundoff = int(sqrt) + square = roundoff**2 + return square diff --git a/pokedex.py b/pokedex.py new file mode 100644 index 0000000..025b29a --- /dev/null +++ b/pokedex.py @@ -0,0 +1,50 @@ +import PySimpleGUI as sg +import requests, json + +sg.theme('DarkAmber') + +# Define the window's contents +layout = [[sg.Text("What's the name of the pokemon?")], + [sg.Input(key='-INPUT-')], + [sg.Button('Ok'), sg.Button('Quit')]] + +# Create the window +window = sg.Window('Pokedex', layout) + +# Display and interact with the Window using an Event Loop +while True: + event, values = window.read() + # See if user wants to quit or window was closed + if event == sg.WINDOW_CLOSED or event == 'Quit': + window.close() + break + elif event == 'Ok': + try: + pokename = requests.get('https://pokeapi.co/api/v2/pokemon/{}/'.format(values['-INPUT-'])) + pokedata = json.loads(pokename.text) + + poketype = pokedata['types'][0]['type']['name'] + + sg.Print('{} type'.format(poketype)) + + req = requests.get(pokedata['types'][0]['type']['url']) + damage_data = json.loads(req.text) + double_damage = damage_data['damage_relations']['double_damage_from'] + sg.Print('{} takes double damage from these types of pokemon'.format(values['-INPUT-']),text_color='white') + for damage in double_damage: + sg.Print(damage['name']) + + for elements in double_damage: + url = elements['url'] + req = requests.get(url) + double_damage_data = json.loads(req.text) + sg.Print('{} takes double damage from these {} pokemon'.format(values['-INPUT-'], elements['name']),text_color='white') + for i in range(5): + sg.Print(double_damage_data['pokemon'][i]['pokemon']['name']) + + sg.Print("{}'s abilities".format(values['-INPUT-']),text_color='white') + for ability in pokedata['abilities']: + sg.Print(ability['ability']['name']) + except Exception as exc: + sg.Print('{} does not exist. Try again'.format(values['-INPUT-'])) + diff --git a/step4-datastructures.py b/step4-datastructures.py new file mode 100644 index 0000000..55cf7e4 --- /dev/null +++ b/step4-datastructures.py @@ -0,0 +1,21 @@ +import time + +def datastructuresearch(subset, universal): + """Get intersection of two lists with set().intersection() method +set(list1).intersection(list2) returns a list which is an intersection of the two lists. +The length of the list gives the number of elements in list1 common with list2 +""" + start = time.time() + print(len(set(subset_elements).intersection(all_elements))) + print('Duration: {} seconds'.format(time.time() - start)) + +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') + +print(datastructuresearch.__doc__) +datastructuresearch(subset_elements, all_elements) + + diff --git a/step4-numpy.py b/step4-numpy.py new file mode 100644 index 0000000..ac458a8 --- /dev/null +++ b/step4-numpy.py @@ -0,0 +1,28 @@ +import time +import numpy as np + +def numpysearch(subset, universal): + """Search elements faster with numpy +np.in1d(list1,list2) returns a list with boolean values True and False. +True if the element in list1 is present in list2, and False if there isn't. +The True elements are appended to a list called verified. +The length of the list gives the number of elements present in list2 which is common with list1. +""" + start = time.time() + verified = [] + for boolvalue in np.in1d(subset,universal): + if boolvalue: + verified.append(boolvalue) + print(len(verified)) + print('Duration: {} seconds'.format(time.time() - start)) + +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') + +print(numpysearch.__doc__) +numpysearch(subset_elements, all_elements) + +