From 991ea1c3fbf342a8dcff22f1cc51f6d7c8f4a0bb Mon Sep 17 00:00:00 2001 From: Akhil S Kumar <54428228+akhil-s-kumar@users.noreply.github.com> Date: Sun, 10 Jan 2021 20:58:49 +0530 Subject: [PATCH] commit changes --- Python-ifed.ipynb | 242 ++++++++++++++++++++++++++++++++--------- ability.py | 14 +++ nearest_square.py | 26 +++++ pokedex.py | 59 ++++++++++ step_4.py | 92 ++++++++++++++++ test_abilities.py | 27 ++--- test_nearest_square.py | 21 ++-- 7 files changed, 406 insertions(+), 75 deletions(-) create mode 100644 ability.py create mode 100644 nearest_square.py create mode 100644 pokedex.py create mode 100644 step_4.py diff --git a/Python-ifed.ipynb b/Python-ifed.ipynb index ee9444d..6aef36a 100644 --- a/Python-ifed.ipynb +++ b/Python-ifed.ipynb @@ -50,8 +50,6 @@ ] }, { - "cell_type": "markdown", - "metadata": {}, "source": [ "# Step 1\n", "\n", @@ -62,16 +60,18 @@ "### Describe what each of the following means\n", "\n", "#### Production\n", - "(Insert answer here)\n", + "Production programming involves creating or updating and testing software product to meet the needs of real customers.\n", "#### Clean \n", - "(Insert answer here)\n", + "Clean code is 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", + "Modular programming is the process of subdividing a computer program into separate sub-programs.\n", "#### Module\n", - "(Insert answer here)\n", + "A module is a separate software component. It can often be used in a variety of applications. It reduces its complexity to some degree and it creates a number of well-defined, documented boundries within the program.\n", "#### Refactoring code\n", - "(Insert answer here)" - ] + "Refactoring is a systematic process of improving code without creating new functionality that can transform a mess into clean code and simple design." + ], + "cell_type": "markdown", + "metadata": {} }, { "cell_type": "markdown", @@ -167,11 +167,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "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 " + ], + "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
" + }, + "metadata": {}, + "execution_count": 39 + } + ], "source": [ - "# Insert your solution here" + "labels = list(df.columns)\n", + "df.columns = [list(df.columns)[i].replace(' ', '_') for i in range(len(labels))] \n", + "df.head()" ] }, { @@ -227,15 +279,22 @@ " subset_elements = f.read().split('\\n')\n", " \n", "with open('all_elements.txt') as f:\n", - " all_elements = f.read().split('\\n')\n", - " " + " all_elements = f.read().split('\\n') " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "96\nDuration: 14.42495083808899 seconds\n" + ] + } + ], "source": [ "start = time.time()\n", "verified_elements = []\n", @@ -257,11 +316,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "96\nDuration: 0.03100132942199707 seconds\n" + ] + } + ], "source": [ - "# Insert answer here\n", + "start = time.time()\n", + "a = np.array(subset_elements)\n", + "b = np.array(all_elements)\n", + "verified_elements = a[np.in1d(a, b)]\n", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -276,11 +346,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "96\nDuration: 0.006000041961669922 seconds\n" + ] + } + ], "source": [ - "# Insert answer here\n", + "start = time.time()\n", + "a = set(subset_elements)\n", + "b = set(all_elements)\n", + "verified_elements = a.intersection(b)\n", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -386,11 +467,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "water\n" + ] + } + ], "source": [ - "# Insert your code here" + "import requests\n", + "import json\n", + "\n", + "name = 'psyduck'\n", + "url = 'https://pokeapi.co/api/v2/pokemon/%s'%(name)\n", + "response = requests.get(url)\n", + "type_data =json.loads(response.text)\n", + "print(type_data['types'][0]['type']['name'])" ] }, { @@ -404,11 +500,30 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "grass\nelectric\nsteel\nfire\nwater\nice\n" + ] + } + ], "source": [ - "# Insert your code here" + "import requests\n", + "import json\n", + "\n", + "url = 'https://pokeapi.co/api/v2/type/11/'\n", + "response = requests.get(url)\n", + "type_data =json.loads(response.text)\n", + "a = len(type_data['damage_relations']['double_damage_from'])\n", + "b = len(type_data['damage_relations']['half_damage_from'])\n", + "for i in range(a):\n", + " print(type_data['damage_relations']['double_damage_from'][i]['name'])\n", + "for i in range(b):\n", + " print(type_data['damage_relations']['half_damage_from'][i]['name'])" ] }, { @@ -420,11 +535,45 @@ }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], + "execution_count": 45, + "metadata": { + "tags": [] + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "bulbasaur It's type is grass\n", + "ivysaur It's type is grass\n", + "venusaur It's type is grass\n", + "oddish It's type is grass\n", + "gloom It's type is grass\n", + "pikachu It's type is electric\n", + "raichu It's type is electric\n", + "magnemite It's type is electric\n", + "magneton It's type is electric\n", + "voltorb It's type is electric\n" + ] + } + ], "source": [ - "# Insert your code here" + "import requests\n", + "import json\n", + "\n", + "url = 'https://pokeapi.co/api/v2/type/11/'\n", + "response = requests.get(url)\n", + "type_data =json.loads(response.text)\n", + "a = len(type_data['damage_relations']['double_damage_from'])\n", + "b = []\n", + "for i in range(a):\n", + " b.append(type_data['damage_relations']['double_damage_from'][i]['name'])\n", + "for j in b:\n", + " url2 = 'https://pokeapi.co/api/v2/type/%s'%(j)\n", + " response2 = requests.get(url2)\n", + " type_data2 =json.loads(response2.text)\n", + " for k in range(5):\n", + " print(type_data2['pokemon'][k]['pokemon']['name'], \"It's type is\", j)" ] }, { @@ -438,24 +587,9 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "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" - ] - } - ], + "outputs": [], "source": [ "!pytest test_abilities.py" ] @@ -501,9 +635,13 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" + "name": "python3", + "display_name": "Python 3.7.1 32-bit", + "metadata": { + "interpreter": { + "hash": "8e61c9422603e3d3820d3aeca1b8eefe471d1a1023a28a3903412dc32402bf43" + } + } }, "language_info": { "codemirror_mode": { @@ -515,7 +653,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.7.1-final" } }, "nbformat": 4, diff --git a/ability.py b/ability.py new file mode 100644 index 0000000..a091ef9 --- /dev/null +++ b/ability.py @@ -0,0 +1,14 @@ +import requests +import json + +def ability(name_str): + name = name_str + url = 'https://pokeapi.co/api/v2/pokemon/%s'%(name) + response = requests.get(url) + type_data =json.loads(response.text) + a = len(type_data['abilities']) + lst_ability = [] + for i in range(a): + lst_ability.append(type_data['abilities'][i]['ability']['name']) + return lst_ability + diff --git a/nearest_square.py b/nearest_square.py new file mode 100644 index 0000000..3ed7e88 --- /dev/null +++ b/nearest_square.py @@ -0,0 +1,26 @@ +import math +# Importing math for mathematical functions + +def nearest_square(n): + '''nearest_square + + Parameters: + n (int): To get the nearest square below n + + Returns: + int 0 : if n is less than 0 + int i : nearest square number but less than n + + ''' + if n<0: + return 0 + else: + + # To find the square of the given number n + root = math.sqrt(n) + for i in range(n,-1,-1): + if int(root)**2 ==i: + return i + else: + continue + diff --git a/pokedex.py b/pokedex.py new file mode 100644 index 0000000..b8b7de3 --- /dev/null +++ b/pokedex.py @@ -0,0 +1,59 @@ +import PySimpleGUI as sg +import requests +import json + +layout = [[sg.Text("Pokemon Name")], + [sg.Input(key='-INPUT-')], + [sg.Text(size=(40,1), key='-OUTPUT1-')], + [sg.Text(size=(40,1), key='-OUTPUT2-')], + [sg.Text(size=(80,1), key='-OUTPUT3-')], + [sg.Button('Ok'), sg.Button('Quit')]] + +window = sg.Window('Pokedex', layout) + +# To get Type Name +name = 'psyduck' +url = 'https://pokeapi.co/api/v2/pokemon/%s'%(name) +response = requests.get(url) +type_data =json.loads(response.text) +typename = type_data['types'][0]['type']['name'] + +#To get type of pokemon gives double damages to the given pokemon +url = 'https://pokeapi.co/api/v2/type/%s'%(typename) +response = requests.get(url) +type_data =json.loads(response.text) +a = len(type_data['damage_relations']['double_damage_from']) +b = ' ' +for i in range(a): + c = type_data['damage_relations']['double_damage_from'][i]['name'] + b += ', '+c + +# 5 pokemons which gives the given pokemon double damage +url = 'https://pokeapi.co/api/v2/type/%s'%(typename) +response = requests.get(url) +type_data =json.loads(response.text) +e = len(type_data['damage_relations']['double_damage_from']) +d = [] +for i in range(e): + d.append(type_data['damage_relations']['double_damage_from'][i]['name']) +for j in d: + url2 = 'https://pokeapi.co/api/v2/type/%s'%(j) + response2 = requests.get(url2) + type_data2 =json.loads(response2.text) + f = ' ' + for k in range(5): + g = type_data2['pokemon'][k]['pokemon']['name'] + f += ', '+g + + +while True: + event, values = window.read() + if event == sg.WINDOW_CLOSED or event == 'Quit': + break + + window['-OUTPUT1-'].update('Type ' + typename ) + window['-OUTPUT2-'].update('Double Damage:' + b ) + window['-OUTPUT3-'].update('Pokemons Double Damage:' + f ) + + +window.close() diff --git a/step_4.py b/step_4.py new file mode 100644 index 0000000..6ec5253 --- /dev/null +++ b/step_4.py @@ -0,0 +1,92 @@ +import time +# Time module provides many ways of representing time +import pandas as pd +# Used for Data Structure +import numpy as np +# For multi dimensional array + +def uForLoop(lst1, lst2): + '''uForLoop + + Parameters: + lst1 (list): List of elements to be checked with list 2 + lst2 (list): List of all elements + + Prints: + len(verified_elements) : Number of elements which are there in list 2 + time.time() - start: Total time to execute the loop + ''' + + start = time.time() + verified_elements = [] # Empty list + + for element in lst1: + if element in lst2: + verified_elements.append(element) # Adding element to the list + + print(len(verified_elements)) + print('Duration: {} seconds, is used by for loop'.format(time.time() - start)) + + +def uNumpy(lst1, lst2): + '''uForLoop + + Parameters: + lst1 (list): List of elements to be checked with list 2 + lst2 (list): List of all elements + + Prints: + len(verified_elements) : Number of elements which are there in list 2 + time.time() - start: Total time to execute the loop + ''' + start = time.time() + + # Creating a 1D array for lst1 and lst2 + a = np.array(lst1) + b = np.array(lst2) + + verified_elements = a[np.in1d(a, b)] # Finds all elements in b that has elements in a + + print(len(verified_elements)) + print('Duration: {} seconds, is used by numpy'.format(time.time() - start)) + + +def uSet(lst1, lst2): + '''uForLoop + + Parameters: + lst1 (list): List of elements to be checked with list 2 + lst2 (list): List of all elements + + Prints: + len(verified_elements) : Number of elements which are there in list 2 + time.time() - start: Total time to execute the loop + ''' + + start = time.time() + a = set(lst1) + b = set(lst2) + verified_elements = a.intersection(b) # Finding Elements in b that has a + + print(len(verified_elements)) + print('Duration: {} seconds, is used by set'.format(time.time() - start)) + +def main(): + with open('subset_elemets.txt') as f: + subset_elements = f.read().split('\n') + + with open('all_elements.txt') as f: + all_elements = f.read().split('\n') + + # Calling uForLoop function + uForLoop(subset_elements, all_elements) + + # Calling uNumpy function + uNumpy(subset_elements, all_elements) + + # Calling uSet function + uSet(subset_elements, all_elements) + +# Calls main function +if __name__=="__main__": + main() \ No newline at end of file diff --git a/test_abilities.py b/test_abilities.py index eaa6157..ba88226 100644 --- a/test_abilities.py +++ b/test_abilities.py @@ -1,13 +1,14 @@ -from ability import ability - -def test_ability_machamp(): - assert(ability("machamp")==['guts', 'no-guard', 'steadfast']) - -def test_ability_pikachu(): - assert(ability("pikachu")==['static', 'lightning-rod']) - -def test_ability_charizard(): - assert(ability("charizard")==['blaze', 'solar-power']) - -def test_ability_evee(): - assert(ability("eevee")==['run-away', 'adaptability', 'anticipation']) \ No newline at end of file +import pytest +from ability import ability + +def test_ability_machamp(): + assert(ability("machamp")==['guts', 'no-guard', 'steadfast']) + +def test_ability_pikachu(): + assert(ability("pikachu")==['static', 'lightning-rod']) + +def test_ability_charizard(): + assert(ability("charizard")==['blaze', 'solar-power']) + +def test_ability_evee(): + assert(ability("eevee")==['run-away', 'adaptability', 'anticipation']) diff --git a/test_nearest_square.py b/test_nearest_square.py index c5d1e43..00154fb 100644 --- a/test_nearest_square.py +++ b/test_nearest_square.py @@ -1,10 +1,11 @@ -from nearest_square import nearest_square - -def test_nearest_square_5(): - assert(nearest_square(5)==4) -def test_nearest_square_n12(): - assert(nearest_square(-12)==0) -def test_nearest_square_9(): - assert(nearest_square(9)==9) -def test_nearest_square_23(): - assert(nearest_square(23)==16) \ No newline at end of file +import pytest +from nearest_square import nearest_square + +def test_nearest_square_5(): + assert(nearest_square(5)==4) +def test_nearest_square_n12(): + assert(nearest_square(-12)==0) +def test_nearest_square_9(): + assert(nearest_square(9)==9) +def test_nearest_square_23(): + assert(nearest_square(23)==16)