Functions You must implement the following functions. Examples can be found late
ID: 3823906 • Letter: F
Question
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 ofthe file always ends with a newline Nn 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.Explanation / Answer
def stripQuotes(s):
if not s:
return None
return s[1:-1]
def getBooleanValue(s):
return stripQuotes(s) == "True"
def read_info_file(filename):
info_db = {}
with open(filename, 'r') as f:
header = f.readline().strip().split(",")
length = len(header)
for line in f:
if not line.strip():
continue
entry = line.strip().split(",")
pokemon = {}
name = ""
if len(entry) == 7:
name = "".join(entry[1:3])
else:
name = entry[1]
info_db[stripQuotes(name)] = (int(entry[0]),
stripQuotes(entry[-4]),
stripQuotes(entry[-3]),
int(entry[-2]),
getBooleanValue(entry[-1]))
return info_db
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
# pastebin link for code: https://pastebin.com/kRf0QpTU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.