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

Python 3 Coding Functions: I need help coding the functions listed below in the

ID: 3820768 • Letter: P

Question

Python 3 Coding Functions:

I need help coding the functions listed below in the image:

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) a 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. t 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 legendary_count_of_types(db):
types = get_types(db)
legendary = {}
for type in types:
legendary[type] = 0
for type in types:
for pokemon in db:
if ((db[pokemon][1] and type == db[pokemon][1]) or (db[pokemon][2] and type == db[pokemon][2])) and db[pokemon][-1]:
legendary[type] += 1
return legendary

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

# code link: https://paste.ee/p/rjlrZ