In Python: What\'s allowed? Here is the exhaustive list of things you can use on
ID: 3823624 • Letter: I
Question
In Python:
What's allowed? Here is the exhaustive list of things you can use on the project. You can ask ifwe've omitted something, but the answer is probably no. all basic expressions/operators, indexinghslicing. all basic statements: assignment, selection, and loop statements, break/continue, return functions: len(), range(), int(), float str(), set(), dict(), bool(), tuple() file reading: open() close() read(), readline() readlines(), with syntax dictionaries: all methods listed in our slides on that chart methods: lists: insert() append() extend pop() remove() strings: .strip split() join() sorted(), sort(), reversed() reverse() This means that... you can't call anything not listed above. Focus on applying these functions to solve the task you can't import any modules for this project (e.g, you can't import csv) you can't use try-except for this assignment Procedure Complete the function definitions as described. you can test your code with the included testing file Sample csv files have been provided with this assignment You can look for example file contents & databases, starting around line 75 or so in the tester. Invoke it as with prior assignments: python 3 tester5p.py yourcode.py You can also test individual functions: python tester5p.py yourcode-py get-types You can also run your code in interactive mode: python3 -i yourcode.py Note that there are 64 test cases worthl.25 points each, and 5 extra credit tests worth 1 point each. Scenario The Pokemon franchise consists of over 60 video games, 15 movies, several TV shows, atrading card game, and even a musical. It has been around for over two decades and at this point has fans ofall ages. Because of this, people have become somewhat analytical about how they play the games. To help players of the Pokemon video games, some people have created a Pokemon data set with all the useful statistics, while other people have created data sets with the Pokemon's other properties (such as type). It will be your job to merge together these two data sets and give players some useful statistics and analysis of what you find. CSV file: This is a file containing ASCII text where each line in the file represents one record of information, and each piece of info in the record is separated by a single comma. The very first line is the header" row, which names the columns but is not part of the data. You will be given two CSV files to work with: an "info" file (containing information such as the Pokemon's type" and the "generation" ofthe game it was introduced in) as well as a "stats" file (containing information on how good the Pokemon is at various things, such as attack" and defense') Below are two very small sample files that can be used in our project Note: the file extension has no effect on the file contents, you can edit these files in your code editor, and you can give them any extension you want without changing the ability of your program. It's best not to use MS Excel, as it often uses several different notions of what a CSV file should be and it is easy to mess them upExplanation / Answer
# code link : http://pasted.co/91fdda75
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 read_stats_file(filename):
stats_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(",")
stats_db[int(entry[0])] = (int(entry[1]),
int(entry[2]),
int(entry[3]),
int(entry[4]))
return stats_db
def combine_databases(info_db, stats_db):
db = {}
for pokemon in info_db:
pokrecord = info_db[pokemon]
if pokrecord[0] in stats_db:
statsrecord = stats_db[pokrecord[0]]
else:
continue
record = (pokrecord[0], pokrecord[1], pokrecord[2]) + statsrecord + (pokrecord[3], pokrecord[4])
db[pokemon] = record
return db
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 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 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.