diff --git a/Python-ifed.ipynb b/Python-ifed.ipynb index ee9444d..0a2d40b 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", + "It is code that runs on a production server, which means it is being used by the intended audience \n", "#### Clean \n", - "(Insert answer here)\n", + "It is a readable, simple and concise code \n", "#### Modular\n", - "(Insert answer here)\n", + "Code broken up into functions and modules which makes it more organized and reusable.\n", "#### Module\n", - "(Insert answer here)\n", + "It allows the code to be reused in the same file or can be reused by other files by importing it.\n", "#### Refactoring code\n", - "(Insert answer here)" + "It is the restructuring of code without changing its external behavior. " ] }, { @@ -171,7 +171,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert your solution here" + "# labels = list(df.columns)", + "labels = [word.replace(' ','_') for word in labels]", + "df.columns=labels" ] }, { @@ -261,7 +263,9 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert answer here\n", + "start = time.time()" + "verified_elements = []" + "verified_elements = np.intersect1d(subset_elements,all_elements)", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -280,7 +284,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert answer here\n", + "start = time.time()" + "verified_elements = set(subset_elements) & set(all_elements)\n", "\n", "print(len(verified_elements))\n", "print('Duration: {} seconds'.format(time.time() - start))" @@ -390,7 +395,14 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert your code here" + "import pokebase as pb", + "import requests,json", + "def getType():", + " type_response = requests.get('https://pokeapi.co/api/v2/pokemon/psyduck')", + " text = json.loads(type_response.text)", + " for item in text['types']:", + " print(item['type']['name'])\n", + "getType()" ] }, { @@ -408,7 +420,25 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert your code here" + "import pokebase as pb", + "import requests,json", + "def getType():", + " type_response = requests.get('https://pokeapi.co/api/v2/pokemon/psyduck')", + " text = json.loads(type_response.text)", + " type_url = []", + " for item in text['types']:", + " type_url.append(item['type']['url'])", + " return type_url\n", + "def get_weakness():", + " urls = getType()", + " for link in urls:", + " affect_res = requests.get(link)", + " affect_json = json.loads(affect_res.text)", + " print('Type :',affect_json['name'])", + " for values in affect_json['damage_relations']['double_damage_from']:", + " weakness = values['name']", + " print('weakness :',weakness)", + "get_weakness()" ] }, { @@ -424,7 +454,40 @@ "metadata": {}, "outputs": [], "source": [ - "# Insert your code here" + "import pokebase as pb", + "import requests,json", + "def getType():", + " type_response = requests.get('https://pokeapi.co/api/v2/pokemon/psyduck')", + " text = json.loads(type_response.text)", + " type_url = []", + " for item in text['types']:", + " type_url.append(item['type']['url'])", + " return type_url\n", + "def get_weakness():", + " urls = getType()", + " weak_types = []", + " for link in urls:", + " affect_res = requests.get(link)", + " affect_json = json.loads(affect_res.text)", + " print('Type :',affect_json['name'])", + " for values in affect_json['damage_relations']['double_damage_from']:", + " weakness = values['name']", + " weak_types.append(weakness)", + " return weak_types\n", + "def get_damage_pokemon():" + " types = get_weakness()" + " print('This pokemon is weak agianst pokemon: ')" + " for type in types:" + " count = 0" + " strong_response = requests.get('https://pokeapi.co/api/v2/type/{}/'.format(type))" + " strong_json = json.loads(strong_response.text)" + " print(type,':')" + " for data in strong_json['pokemon']:" + " print(data['pokemon']['name'])" + " count+=1" + " if count >= 5:" + " break" + "get_damage_pokemon()" ] }, { diff --git a/ability.py b/ability.py new file mode 100644 index 0000000..f2191c6 --- /dev/null +++ b/ability.py @@ -0,0 +1,10 @@ +import pokebase as pb +import requests,json + +def ability(name): + res = requests.get("https://pokeapi.co/api/v2/pokemon/{}".format(name)) + data = json.loads(res.text) + abilities = [] + for values in data['abilities']: + abilities.append(values['ability']['name']) + return abilities diff --git a/get_data.py b/get_data.py new file mode 100644 index 0000000..0b2dea0 --- /dev/null +++ b/get_data.py @@ -0,0 +1,107 @@ +import pokebase as pb +import requests,json + +def getType(name): + """ + Returns the type of the input pokemon + + Parameters: + ------------ + name : str + + Returns: + ----------- + list of all types of the input pokemon + """ + type_response = requests.get('https://pokeapi.co/api/v2/pokemon/{}'.format(name)) + text = json.loads(type_response.text) + type_name = [] + for item in text['types']: + type_name.append(item['type']['name']) + return type_name + +def getType_url(name): + """ + Returns the api url of the input pokemon types. + + Parameters: + ------------ + name : str + + Returns: + ----------- + list of all url for types + """ + type_response = requests.get('https://pokeapi.co/api/v2/pokemon/{}'.format(name)) + text = json.loads(type_response.text) + type_url = [] + for item in text['types']: + type_url.append(item['type']['url']) + return type_url + +def get_weakness(name): + """ + Returns the types that the input pokemon is weak against. + + Parameters: + ------------ + name : str + + Returns: + ----------- + list of all the types the input pokemon is weak against + """ + urls = getType_url(name) + weak_types = [] + for link in urls: + affect_res = requests.get(link) + affect_json = json.loads(affect_res.text) + for values in affect_json['damage_relations']['double_damage_from']: + weakness = values['name'] + weak_types.append(weakness) + return weak_types + +def get_damage_pokemon(name): + """ + Returns the name of the pokemons that the input pokemon is weak against + + Parameters: + ------------ + name : str + + Returns: + ----------- + list of the names of pokemon the input pokemon is weak against + """ + types = get_weakness(name) + pokemons = [] + for type in types: + count = 0 + strong_response = requests.get('https://pokeapi.co/api/v2/type/{}/'.format(type)) + strong_json = json.loads(strong_response.text) + for data in strong_json['pokemon']: + pokemons.append(data['pokemon']['name']) + count+=1 + if count >= 5: + break + return pokemons + +def ability(name): + """ + Returns the abilities of the input pokemon + + Parameters: + ------------ + name : str + + Returns: + ----------- + list of all the abilities of input pokemon + """ + res = requests.get("https://pokeapi.co/api/v2/pokemon/{}".format(name)) + data = json.loads(res.text) + abilities = [] + for values in data['abilities']: + abilities.append(values['ability']['name']) + return abilities + diff --git a/nearest_square.py b/nearest_square.py new file mode 100644 index 0000000..e3ca72e --- /dev/null +++ b/nearest_square.py @@ -0,0 +1,9 @@ +import math + +def nearest_square(number): + try: + nearest = math.floor(math.sqrt(number)) + return nearest*nearest + except ValueError: + return 0 + diff --git a/pokedex.py b/pokedex.py new file mode 100644 index 0000000..1ab2740 --- /dev/null +++ b/pokedex.py @@ -0,0 +1,21 @@ +import PySimpleGUI as sg +import get_data as pk + +layout = [[sg.Text("Enter pokemon: ")], + [sg.Input(key='-INPUT-')], + [sg.Text(size=(40,1), key='-OUTPUT-')], + [sg.Button('Ok'), sg.Button('Quit')]] +window = sg.Window('Pokedex',layout) +while True: + event, values = window.read() + if event == sg.WINDOW_CLOSED or event == 'Quit': + break + pokeName = values['-INPUT-'] + sg.popup('Name:',pokeName, + 'Type: ', pk.getType(pokeName), + 'Weakness: ',pk.get_weakness(pokeName), + 'Weak against: ',pk.get_damage_pokemon(pokeName), + 'Abilities: ',pk.ability(pokeName) + ) + +window.close() diff --git a/step_4.py b/step_4.py new file mode 100644 index 0000000..84ed7a6 --- /dev/null +++ b/step_4.py @@ -0,0 +1,55 @@ +import time +import numpy as np + +def usingNumpy(all_elem, subset): + """ + It gives the length of an array containing the common + values of two arrays using the numpy module + + Parameters: + ----------- + all_elem: array + subset: array + + Returns: + ----------- + Length of array containing the common elements of arrays passed in through the + arguments + + """ + verified_elements = np.intersect1d(subset,all_elem) + return len(verified_elements) + +def usingNormal(all_elem,subset): + """ + It gives the length of an array containing the common + values of two arrays using the inbuilt fuctions of python + + Parameters: + ----------- + all_elem: array + subset: array + + Returns: + ----------- + Length of array containing the common elements of arrays passed in through the + arguments + + """ + verified_elements = set(all_elem) & set(subset) + return len(verified_elements) + +if __name__ == "__main__": + + with open('subset_elemets.txt') as f: + subset_elements = f.read().split('\n') + + with open('all_elements.txt') as f: + all_elements = f.read().split('\n') + + start = time.time() + all_elements = np.array(all_elements) + + print(usingNumpy(all_elements, subset_elements)) + print(usingNormal(all_elements, subset_elements)) + print('Duration: {} seconds'.format(time.time() - start)) \ No newline at end of file