Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 Coding Functions: Here is any code required to help code what is below:

ID: 3823265 • Letter: P

Question

Python 3 Coding Functions:

Here is any code required to help code what is below:

def pokemon_by_types(db, types):
new_db = {}
for pokemon_type in types:
for key in db: # iterate through all the type in types list
if db[key][1] == pokemon_type or db[key][2] == pokemon_type:
if key not in new_db:
new_db[key] = db[key]
return new_db

Note: PLEASE DO NOT ANSWER UNLESS IT IS RELEVANT TO THE CODING.

I need help coding the functions listed below:

get_types(db): Given a database db, this function determines all the pokemon types in the database. It returns the types as a list of strings, asciibatically sorted (in order based on the ASCII character values). The sorted() function or .sort() method can be helpful here.

count_by_type(db,type): Given a database db and a single pokemon type (as a string) this function collects and reports three statistics

l.how many pokemon in db have type as their only type

2. how many dual-type pokemon in db have type as one of their two types

3.a sum of the two values (1 and 2) above

A tuple of (single type count, dual type count, total count) should be returned.

fastest_type(db): Given a database db, determine the type with the highest average speed. Ties are possible, so return a list of types (strings) sorted asciibatically. Hints: The sorted function or .sort() method can be helpful here, as can get_types() and pokemon_by_types( )

legendary_count_of_types(db): Given a database db, for every type in that database, count how many pokemon of that type are legendary. Create a new dictionary to report the counts. It should have one entry for every type in the original database and be structured in the format: type: count of legendary. For example, { "Fire": 2, "Ground": 1 }. Hint: get_types and pokemon_by_types() could be helpful here.

team_hp(db, team): Given a database db and a list of pokemon names (as strings) team, find out the total hps of all pokemon on that team and return it as an integer. Assume all pokemon on the team are included in the database

Explanation / Answer

def get_types(db):
type_list = []
for pokemon in db:
if db[pokemon][1] and not db[pokemon][1] in type_list:
type_list.append(db[pokemon][1])
if db[pokemon][2] and not db[pokemon][2] in type_list:
type_list.append(db[pokemon][2])
if type_list:
type_list.sort()
return type_list
  
def count_by_type(db, pok_type):
single = 0
double = 0
for pokemon in db:
if (db[pokemon][1] and pok_type == db[pokemon][1]) or (db[pokemon][2] and pok_type == db[pokemon][2]):
if (not db[pokemon][1] and db[pokemon][2]) or (db[pokemon][1] and not db[pokemon][2]):
single += 1
if db[pokemon][1] and db[pokemon][2]:
double += 1
return (single, double, single+double)

def fastest_type(db):
types = get_types(db)
avg_speed = {}
for type in types:
avg_speed[type] = []
for type in types:
for pokemon in db:
if db[pokemon][1]:
avg_speed[db[pokemon][1]].append(db[pokemon][-3])
if db[pokemon][2]:
avg_speed[db[pokemon][2]].append(db[pokemon][-3])
avg = 0
for type in types:
if avg_speed[type]:
avg_speed[type] = sum(avg_speed[type])/len(avg_speed[type])
else:
avg_speed[type] = 0
if avg < avg_speed[type]:
avg = avg_speed[type]
types = []
for type in avg_speed:
if avg_speed[type] == avg:
types.append(type)
types.sort()
return types

def team_hp(db, team):
total_hp = 0
for pokemon in team:
total_hp += db[pokemon][3]
return total_hp

# pastebin linik for code: https://pastebin.com/PEU83ATL

Please rate positively.