Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is my code: def read _ pokedata ( poke _ file ) : poke _ file = open ( poke _ file, ' r

this is my code: def read_pokedata(poke_file):
poke_file = open(poke_file, 'r')
poke_data ={}
"""takes parameter poke_file and returns a dictionary of the records of each pokemon with
pokemon name as the key and data as its value
poke_file: file consisting of pokemons name, type and location """
for line in poke_file:
# split any newline characters in the file
spaces = line.strip()
# separate each value that has a comma and turn it into list
items = spaces.split(',')
# name, type and location based on the indexes of the list
pokemon_name = items[0]
pokemon_type = items[1]
pokemon_location =','.join(items[2:])
# pokemon name as the key with separate dictionary of pokemon type and locations as value
poke_data[pokemon_name]={'pokemon type': pokemon_type, 'location(s)': pokemon_location }
return poke_data
#poke_data = read_pokedata(poke_file)
def find_continents(poke_data):
""" defines function find_continents with parameter poke_data and returns list of different
locations pokemon's can be found in.
poke_data: dictionary consisting of pokemon name its key and separate dictionary with type and locations as value """
# for pokemon name and data within that name turn the values within data into list
continent_list =[]
for name, data in poke_data.items():
# for each continent in the second index of that value location(s)
for location in data['location(s)'].split(','):
# append unique continent into continent_list if its not already in the list
if location.strip() not in continent_list:
continent_list.append(location.strip())
return continent_list
def pokemon_in_continent(poke_data, continent_list):
"""takes function pokemon_in_continent and returns list of pokemons in every continent within continent list
poke_data: dictionary of pokemon name with pokemon type and location
content_list: list of continents pokemons are in """
continent_pokemons =[]
# for each continent in continent list and each name and data in dictionary
# if the continent is in the 'location(s)' of a specific pokemon, append that pokemon name in continent_pokemons list
for continent in continent_list:
for name, data in poke_data.items():
if continent in data['location(s)']:
continent_pokemons.append(name)
return continent_pokemons
def count_types(poke_data, continent_pokemons):
"""defines function count_types with two parameters and returns dictionary of number of types within each continent
poke_data: takes dictionary of poke_data
continent_pokemons: takes list of pokemons present in the continent """
poke_types ={}
# for each pokemon in continent_pokemons list
for pokemon in continent_pokemons:
pokemon_type = poke_data[pokemon]['pokemon type']
# if the pokemon type is in poke_types update it by 1
if pokemon_type in poke_types:
poke_types[pokemon_type]+=1
# if the pokemon type is not in poke_types count it by 1
else:
poke_types[pokemon_type]=1
return poke_types
poke_data = read_pokedata(poke_file)
continents = find_continents(poke_data)
# for each continent in continents
for continent in continents:
# defines variable that takes all pokemons inesting Part 1(read_pokedata()): Since this function reads data from a file, your test inputs must
in fact consist of different files! Thus, for each test, create an input file with the same format as
pokemonLocations.txt. Your test files will likely be MUCH smaller than the provided file. Do not bother
testing with files that are not correctly formatted, focus instead on the diversity of possible valid files.
Testing Part 2(find_continents()): The inputs to this function is a database of Pokemon information.
It is ok to use your read_pokedata() function to create the database rather than typing in database
literals (so long as you confident that function works thanks to testing it!), in which case your test inputs
will again consist of input files. Careful! The pokemon in your database might be processed in any
order (because its a dictionary!) and we dont care about the ORDER of the continents in the resulting
list. You can deal with this by SORTING the resulting list before comparing it to the expected result!
Testing Part 3(pokemon_in_continent()): Again this function requires a database, and it is again ok
to use your read_pokedata() funtion to create it if you find this less tedious. The continent name for
each test should however be picked manually. And again, youll want to sort the resulting list before
testing.
Testing Part 4(count_types()): Again this function requires a database, and it is again ok to use your
read_pokedata() funtion to create it if you find this less tedious. The list of pokemon names should be created manually.

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

Students also viewed these Databases questions