Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i keep getting assertion error def read_info_file(filename): info_db = {} with open(filename, 'r') as f: header = f.readline().strip().split(,) for line in reader(f): info_db[int(line[0])] = (

i keep getting assertion error

def read_info_file(filename): info_db = {} with open(filename, 'r') as f: header = f.readline().strip().split(",") for line in reader(f): info_db[int(line[0])] = ( line[1], None if not line[2] else line[2], None if not line[3] else line[3], int(line[4]), True if line[2] == "TRUE" else False) return info_db def read_stats_file(filename): d2={} stats=[] with open(filename,"r") as f: f.readline()#reading headers for line in f: stats=line.split(",") d2[int(stats[0])]=(int(stats[1]),int(stats[2]),int(stats[3]),int(stats[4])) return d2

d1=(read_info_file("info.txt")) d2=(read_stats_file("stats.txt")) def combine_databases(info_db,stats_db): finaldict={} for names in info_db: for id in stats_db: a,b,c,d,e=info_db[names] f,g,h,i=stats_db[id] if(a==id): finaldict[names]=(a,b,c,f,g,h,i,d,e) break

return finaldict print("info_db :%s "%d1) print("stats_db :%s"%d2) print("final_db: %s" % combine_databases(d1,d2)) def pokemon_by_types(db, types): """ :param db: :param types: :return: """ 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 def pokemon_by_hp_defense(db, lowest_hp, lowest_defense): pokemon_dict = {} for pokemon in db: if db[pokemon][3] >= lowest_hp and db[pokemon][5] >= lowest_defense: pokemon_dict[pokemon] = db[pokemon] return pokemon_dict 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):

Fire=0 Grass=0 Flying=0 Poison=0 Dragon=0 Water=0 Fighting=0 Ground=0 Ghost=0 Rock=0 Ice=0 d={} for key,values in db.items(): status=values[8] if status==True: type_list=get_types(db) for item in type_list: if item=='Fire': Fire+=1 if item=='Grass': Grass+=1 if item=='Flying': Flying+=1 if item=='Poison': Poison+=1 if item=='Dragon': Dragon+=1 if item=='Water': Water+=1 if item=='Fighting': Fighting+=1 if item=='Ground': Ground+=1 if item=='Ghost': Ghost+=1 if item=='Rock': Rock+=1 if item=='Ice': Ice+=1 d.update() #how do I get the key value pair? return d def team_hp(db, team): total_hp = 0 for pokemon in team: total_hp += db[pokemon][3] return total_hp def show_of_strength_game(db, team1, team2): len1 = len(team1) len2 = len(team2) score = len1-len2 if len1 > len2: team1 = team1[:len2] elif len2 > len1: team2 = team2[:len1] for i in range(0, len(team1)): if db[team1[i]][4] < db[team2[i]][4]: score -= 1 elif db[team1[i]][4] > db[team2[i]][4]: score += 1 return score def pokemon_by_types(db, pok_type): pokemon_dict = {} for pokemon in db: if db[pokemon][1] and (db[pokemon][1] in pok_type) and (not pokemon in pokemon_dict): pokemon_dict[pokemon] = db[pokemon] if db[pokemon][2] and (db[pokemon][2] in pok_type) and (not pokemon in pokemon_dict): pokemon_dict[pokemon] = db[pokemon] return pokemon_dict def strongest_pokemon(db, type = None, generation = None): pokemon_list = list(db.keys()) if type: pokemon_list = pokemon_by_types(db, type).keys() final_pokemon_list = [] if generation: for pokemon in pokemon_list: if db[pokemon][-2] == generation: final_pokemon_list.append(pokemon) else: final_pokemon_list = pokemon_list pokemon_strength_dict = {} for pokemon in final_pokemon_list: pok = db[pokemon] pokemon_strength_dict[pokemon] = pok[3] + pok[4] + pok[5] max_strength = 0 for pokemon in pokemon_strength_dict: if max_strength < pokemon_strength_dict[pokemon]: max_strength = pokemon_strength_dict[pokemon]

strong_pokemon = [] for pokemon in pokemon_strength_dict: if pokemon_strength_dict[pokemon] == max_strength: strong_pokemon.append(pokemon) strong_pokemon.sort() return strong_pokemon def top_team_with_best_attackers(db, size = 6): attack_dict = {} for pokemon in db: attack_dict[pokemon] = db[pokemon][4] attack_list = sorted(attack_dict.items(), key=lambda x: x[1], reverse=True) attack_list = attack_list[:size] top_teams = [pokemon for pokemon, attack in attack_list] return top_teams

Functions You must implement the following functions. Examples can be found later in this document (under Examples). Methods NEVER modify the given database; but some functions create a new database. read_info_file(filename): This is only one of two functions that deals with reading a file. It accepts the file name as a string, assume it is a CSV file in the format described above for an info file. The function needs to open the file, read all the described pokemon, and create a dictionary of pokemon in the INFO FORMAT. It returns the dictionary it creates. Note: the first line of the file is always a header line which does not corresponds to any pokemon; the last line of the file always ends with a newline (' '). Special case: Name field in the input file might contain one comma as part of the string, for example, "Tornadus, (Incarnate Form)". You can assume the name can have at most one comma and all other fields do not have any comma. 4 read_stats_file(filename): This is the other one of two functions that needs to deal with reading a file. It accepts the file name as a string, assume it is a CSV file in the format described above for a stats file. The function needs to open the file, read all the described pokemon, and create a dictionary of pokemon in the STATS FORMAT. It returns the dictionary it creates. Note: the first line of the file is always a header line which does not corresponds to any pokemon; the last line of the file always ends with a newline (' '). combine_databases(info_db, stats_db): This function takes two dictionaries (one in INFO FORMAT and one in STAT FORMAT) and combines them into a final dictionary database (described in the previous section). Items from one dictionary that do not appear in the other should be discarded. It returns the combined dictionary. NOTE: All functions below will be expecting a full database, not an info or stats dictionary. pokemon_by_types(db, types): This function accepts a pokemon database db and a list of strings types. It needs to build and return a new database with only pokemon of the given types. NOTE: you must not change the original database with this function. pokemon_by_hp_defense(db, lowest_hp, lowest_defense): Given a pokemon database db and two integers indicating the lowest hit points (hp) and lowest defense stats, this function creates and returns a new database consisting only of pokemon with an hp >= lowest_hp and a defense >= lowest_defense. NOTE: you must not change the original database with this function. 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: 1.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. show_of_strength_game(db, team1, team2): Given a database db and two teams (two separate lists of pokemon names), have the teams play out a competition where each pokemon competes against the corresponding pokemon on the other team. Assume all pokemon are listed in the database. A team will win a match if the pokemon on their team has a higher attack stat. If only one team has a pokemon to compete in that slot, that team wins automatically. Return the difference between Team1s score and Team2s score. For example: 5 Team1 = ["Pokemon1", "Pokemon2", "Pokemon3", "Pokemon4"] Team1 = ["Pokemon5", "Pokemon6"] [Team1] [Team2] Pokemon1 Pokemon5 # Pokemon5 has higher attack than Pokemon1, therefore Team2 wins Pokemon2 Pokemon6 # Pokemon2 has higher attack than Pokemon6, therefore Team1 wins Pokemon3 # Team2 ran out of pokemon, so Team1 automatically wins Pokemon4 # Team2 ran out of pokemon, so Team1 automatically wins Team1 wins three times, Team2 wins one time, return 3-1 = 2. strongest_pokemon(db, type = None, generation = None): Given a database of pokemon db, determine the pokemon with the highest total of hp, attack, and defense. If the user decides to restrict the type or generation, provide only the strongest pokemon that also meet those criteria. Since ties are possible, return a list of the strongest pokemon by name, asciibatically sorted. Return None if no pokemon meet the specified requirements. Hint: The sorted() function or .sort() method can be helpful here. Extra Credit top_team_with_best_attackers(db, size=6): Given a database of pokemon db and an integer size, determine the best team that can be made (based on their attack only). The team should always have size members unless the number of pokemon in the original database db is lower than size in which case the team has to include all available pokemon but sorted based on their attack. Break ties asciibetically -- lower asciibetical sorting will come first, e.g. if a Spearow and an Ekans have the same attack power, an Ekans will be chosen before a Spearow. Return the team as a list of pokemon names sorted by the pokemons attack statistic.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learn Mysql The Easy Way A Beginner Friendly Guide

Authors: Kiet Huynh

1st Edition

B0CNY7143T, 979-8869761545

More Books

Students also viewed these Databases questions

Question

Describe the relationship between culture and media.

Answered: 1 week ago

Question

Explain the meanings of reliability and validity.

Answered: 1 week ago