diff --git a/Python-ifed .ipynb b/Python-ifed .ipynb new file mode 100644 index 0000000..33d271e --- /dev/null +++ b/Python-ifed .ipynb @@ -0,0 +1,1074 @@ +{ + "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", + "Your code has to clear multiple stages of testing and debugging before it is put into production. Usually there are three levels: development, staging, and production. In some companies, there will be a level before production that mimics the exact environment of a production system\n", + "#### Clean \n", + "The clean() method on a Field subclass is responsible for running to_python(), validate(), and run_validators() in the correct order and propagating their errors.\n", + "#### Modular\n", + "Modular programming is a software design technique to split your code into separate parts. These parts are called modules. The focus for this separation should be to have modules with no or just few dependencies upon other modules. In other words: Minimization of dependencies is the goal.\n", + "#### Module\n", + "Modules refer to a file containing Python statements and definitions. A file containing Python code, for example: example.py , is called a module, and its module name would be example . We use modules to break down large programs into small manageable and organized files\n", + "#### Refactoring code\n", + "Refactoring is the technique of changing an application (either the code or the architecture) so that it behaves the same way on the outside, but internally has improved. These improvements can be stability, performance, or reduction in complexity." + ] + }, + { + "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": 1, + "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": 1, + "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": 2, + "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": 3, + "metadata": {}, + "outputs": [], + "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": 4, + "metadata": { + "scrolled": false + }, + "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": [ + "df = df.rename(columns=lambda x: x.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": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "import pandas as pd\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 6.562382459640503 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": 8, + "metadata": {}, + "outputs": [], + "source": [ + "\"\"\"importing all libraries\"\"\"\n", + "import time\n", + "import pandas as pd\n", + "import numpy as np\n", + "\"\"\"taking data from subset_elements.txt and all_elements.txt\"\"\"\n", + "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')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "96\n", + "Duration: 0.021625995635986328 seconds\n" + ] + } + ], + "source": [ + "start = time.time()\n", + "verified_elements = np.array([])\n", + "\"\"\" finding the intersection of subset_elements and all_elements\"\"\"\n", + "verified_elements = np.intersect1d(subset_elements, all_elements, assume_unique=True)\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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "24159\n", + "Duration: 0.0036344528198242188 seconds\n", + "24159\n", + "Duration: 0.0037140846252441406 seconds\n" + ] + } + ], + "source": [ + "import time\n", + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "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", + " \n", + "start = time.time()\n", + "verified_elements = set()\n", + "\n", + "for element in subset_elements:\n", + " verified_elements.add(element)\n", + "\n", + "print(len(verified_elements))\n", + "print('Duration: {} seconds'.format(time.time() - start))\n", + "\n", + "print(len(verified_elements))\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": 11, + "metadata": {}, + "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/sushmanth/sushu/Python-ifed-challenge\r\n", + "plugins: xonsh-0.9.13\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" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "normal\n", + "fairy\n" + ] + } + ], + "source": [ + "import requests\n", + "\n", + "url = 'https://pokeapi.co/api/v2/pokemon/jigglypuff'\n", + "\n", + "resp = requests.get(url=url)\n", + "data = resp.json()\n", + "for item in data['types']:\n", + " print(item['type']['name'])\n", + "#Normal and Fairy" + ] + }, + { + "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": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "fighting\n" + ] + } + ], + "source": [ + "import requests\n", + "\n", + "url = 'https://pokeapi.co/api/v2/type/1'\n", + "\n", + "resp = requests.get(url=url)\n", + "data = resp.json()\n", + "for item in data['damage_relations']['double_damage_from']:\n", + " print(item['name'])\n", + "for item in data['damage_relations']['half_damage_from']:\n", + " print(item['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": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "poison\n", + "rock\n", + "steel\n", + "fire\n", + "electric\n" + ] + } + ], + "source": [ + "import requests\n", + "\n", + "url = 'https://pokeapi.co/api/v2/type/5'\n", + "\n", + "resp = requests.get(url=url)\n", + "data = resp.json()\n", + "for item in data['damage_relations']['double_damage_to']:\n", + " print(item['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": 15, + "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/sushmanth/sushu/Python-ifed-challenge\n", + "plugins: xonsh-0.9.13\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.59s\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 deleted file mode 100644 index ee9444d..0000000 --- a/Python-ifed.ipynb +++ /dev/null @@ -1,523 +0,0 @@ -{ - "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", - "(Insert answer here)\n", - "#### Clean \n", - "(Insert answer here)\n", - "#### Modular\n", - "(Insert answer here)\n", - "#### Module\n", - "(Insert answer here)\n", - "#### Refactoring code\n", - "(Insert answer here)" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "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": null, - "metadata": {}, - "outputs": [], - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert your solution here" - ] - }, - { - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "import time\n", - "import pandas as pd\n", - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "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": null, - "metadata": {}, - "outputs": [], - "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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert answer here\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": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert answer here\n", - "\n", - "print(len(verified_elements))\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": null, - "metadata": {}, - "outputs": [], - "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": 1, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert your code here" - ] - }, - { - "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": 2, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For each of the **double damage from** type list 5 pokemons in that type" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "# Insert your code here" - ] - }, - { - "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": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "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", - "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" - ] - } - ], - "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.6.9" - } - }, - "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..4b71371 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..e941fb7 Binary files /dev/null and b/__pycache_/nearest_square.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..0dd569d 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..022b95c Binary files /dev/null and b/__pycache_/test_nearest_square.cpython-38-pytest-6.2.1.pyc differ diff --git a/__pycache_/x b/__pycache_/x new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/__pycache_/x @@ -0,0 +1 @@ + diff --git a/ability.py b/ability.py new file mode 100644 index 0000000..b63def9 --- /dev/null +++ b/ability.py @@ -0,0 +1,14 @@ +import requests + +def ability(name): + url = 'https://pokeapi.co/api/v2/pokemon/' + name + resp = requests.get(url=url) + data = resp.json() + answer = [] + for item in data['abilities']: + answer.append((item['ability']['name'])) + return answer + + +if __name__ == '__main__': + ability("machamp") \ No newline at end of file diff --git a/nearest_square.py b/nearest_square.py new file mode 100644 index 0000000..c621f8d --- /dev/null +++ b/nearest_square.py @@ -0,0 +1,8 @@ +import math +def nearestPerfectSquare(x): + if(x<=0): + return 0 + sr = math.sqrt(x) + pk=math.floor(sr) + pk*=pk + return pk diff --git a/test4_.py b/test4_.py new file mode 100644 index 0000000..916ea72 --- /dev/null +++ b/test4_.py @@ -0,0 +1,23 @@ +"""importing all libraries""" +import time +import pandas as pd +import numpy as np + +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') + """starting the clock""" +start = time.time() +verified_elements = set() +"""adding elements to verified elements""" +for element in subset_elements: + verified_elements.add(element) + +print(len(verified_elements)) +"""calculating duration of intersection of two arrays""" +print('Duration: {} seconds'.format(time.time() - start)) + +print(len(verified_elements)) +print('Duration: {} seconds'.format(time.time() - start)) diff --git a/test_nearest_square.py b/test_nearest_square.py index c5d1e43..9fc0265 100644 --- a/test_nearest_square.py +++ b/test_nearest_square.py @@ -1,10 +1,10 @@ -from nearest_square import nearest_square +from nearest_square import nearestPerfectSquare def test_nearest_square_5(): - assert(nearest_square(5)==4) + assert(nearestPerfectSquare(5)==4) def test_nearest_square_n12(): - assert(nearest_square(-12)==0) + assert(nearestPerfectSquare(-12)==0) def test_nearest_square_9(): - assert(nearest_square(9)==9) + assert(nearestPerfectSquare(9)==9) def test_nearest_square_23(): - assert(nearest_square(23)==16) \ No newline at end of file + assert(nearestPerfectSquare(23)==16)