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

Functions You must implement the following functions. Examples can be found late

ID: 3823783 • 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

import operator
from collections import OrderedDict
import csv
db = {
"Bulbasaur": (1,"Grass", "Poison", 45,49, 49, 45, 1, False),
"Charmander": (4, "Fire", None, 39,52,43,65,1,False),
"Charizard": (6, "Fire", "Flying", 78,84,78,100,1,False),
"Moltres": (146, "Fire", "Flying", 90,100,90,90,1,True),
"Crobat": (169,"Poison", "Flying", 85,90,80,130,2,False),
"Tornadas, (Incarnate Form)": (641,"Flying", None, 79,115,70,111,5,True),
"Reshiram": (643,"Dragon", "Fire", 100,120,100,90,5,True)
}


def read_info_file(filename):
db = {}
with open(filename, 'r') as f:
reader = csv.reader(f)
temp_list = list(map(tuple, reader))
temp_list = temp_list[1:]
for val in temp_list:
if val[0] not in db:
db[val[0]] = (int(val[1]),str(val[2]),str(val[3]),int(val[4]),int(val[5]),int(val[6]),int(val[7]),int(val[7]),int(val[8]),val[9])
return db

def show_of_strength_game(db,team1,team2):
l1 = len(team1)
l2 = len(team2)
team1_count = 0
team2_count = 0
if l1 >= l2:
for i in range(l2):
if db[team1[i]][4] >= db[team2[i]][4]:
team1_count += 1
else:
team2_count += 1
team1_count += l1-l2
if l2 > l1:
for i in range(l1):
if db[team1[i]][4] >= db[team2[i]][4]:
team1_count += 1
else:
team2_count += 1
team2_count += l2 - l1
print("t1count",team1_count)
print("t2count",team2_count)
if team1_count > team2_count:
x = "Team1 Won by " + str(team1_count - team2_count) + " Dragons"
else:
x = "Team2 Won by " + str(team2_count - team1_count) + " Dragons"
  
return x
  


def strongest_pokemon(db, ptype = None, generation = None):
pokemon_list = list(db.keys())   
if ptype:
if generation:
temp_db = {}
for pokemon in db:
if (db[pokemon][1] == ptype or db[pokemon][2] == ptype) and db[pokemon][7] == generation:
if pokemon not in temp_db:
temp_db[pokemon] = db[pokemon][3] + db[pokemon][4] + db[pokemon][5]
temp_list_vals = sorted(temp_db.items(), key = operator.itemgetter(1) )
return temp_list_vals[-1][0]
  
  
  
else:
temp_db = {}
for pokemon in db:
if db[pokemon][1] == ptype or db[pokemon][2] == ptype:
if pokemon not in temp_db:
temp_db[pokemon] = db[pokemon][3] + db[pokemon][4] + db[pokemon][5]
temp_list_vals = sorted(temp_db.items(), key = operator.itemgetter(1) )
return temp_list_vals[-1][0]
  
else:
total_values = {}
for val in pokemon_list:
if val not in total_values:
total_values[val] = db[val][3] + db[val][4] + db[val][5]
temp_list = sorted(total_values.items(),key = operator.itemgetter(1))
return temp_list[-1][0]   
  

def top_team_with_best_attackers(db, size =6):
temp_dict = {}
for pokemon in db:
if pokemon not in temp_dict:
temp_dict[pokemon] = db[pokemon][4]
x = OrderedDict((sorted(temp_dict.items(), key=operator.itemgetter(1), reverse=True)[:size])[::-1])
return list(x.keys())

''' Calling the above methods with arguments to check the correctness'''

top_team_with_best_attackers(db)

read_info_file('info.csv')
  
show_of_strength_game(db,['Bulbasaur', 'Crobat'],['Charmander', 'Charizard', 'Moltres','Tornadas','Reshiram'])