The goal of this assignment is to work on lists and dictionaries in Python.
You will be doing your work in a Jupyter notebook for this
assignment. You may choose to work on this assignment on a hosted
environment (e.g. tiger)
or on your own local installation of Jupyter and Python. You should use
Python 3.10 or higher for your work, but versions 3.9+ should work for
this assignment. To use tiger, use the credentials you received. If you
work remotely, make sure to download the .ipynb file to turn in. If you
choose to work locally, Anaconda is the easiest way
to install and manage Python. If you work locally, you may launch
Jupyter Lab either from the Navigator application or via the
command-line as jupyter-lab
.
In this assignment, we will be working with data from Pokémon compiled from online sources. In this case, we will be using the Complete Pokemon Dataset collected by Mario Tormo Romero from online sources. Rather than using this dataset directly, I have created a subset of this data, which can be read into a list of dictionaries. That data is located here, and I have created a template notebook, a3.ipynb, that contains a cell that will download and read that data. Again, once loaded, the data is a list of dictionaries where each dictionary has ten key-value pairs. Those keys and a brief description are:
name
: the namegeneration
: the generation (1-8)species
: the species of the pokemon (e.g. Lizard
Pokémon)primary_type
: the primary type (e.g. Grass, Fire)hp
: base hit pointsheight_m
: height in metersweight_kg
: weight in kilogramsspeed
: base speedattack
: base attackdefense
: base defenseYou will be answering queries and writing functions to help analyze this data. You may not use external libraries including statistics, collections, or pandas for this assignment.
The assignment is due at 11:59pm on Tuesday, February 21
Friday, February 24.
You should submit the completed notebook file required for this
assignment on Blackboard. The
filename of the notebook should be a3.ipynb
.
Please make sure to follow instructions to receive full credit. Use a markdown cell to Label each part of the assignment with the number of the section you are completing. You may put the code for each part into one or more cells.
The first cell of your notebook should be a markdown cell with a line for your name and a line for your Z-ID. If you wish to add other information (the assignment name, a description of the assignment), you may do so after these two lines.
Write code to find the name of the first Pokemon
with the maxmum number of base hit points (hp
). Remember
that you will need to iterate through each element of the list, and each
element is a dictionary which has various keys including hp
and name
. Update (2023-02-17): There are
two Pokemon with maximum hit points; find the first
one. For extra credit (5 points), find both names
without iterating through the list more than once.
Write code to create a new dictionary generation_counts
that stores the counts of each generation in the dataset.
Write code to create a new dictionary
primary_weights
that stores the mean
weight of all pokemon with the same primary type. Remember that the mean
is the sum of all values divided by the number of values.
weight_kg
is set to None
. Make sure to exclude
them from mean computations.Write a function filter_hp
that takes
three parameters, data
, min_hp
and
max_hp
, and returns a sorted list of
tuples of those pokemon whose base hit points (hp
) is
within the range min_hp <= hp <= max_hp
. The first
entry in each tuple should be the number of hit points of the pokemon,
and the second entry is its name. Set the default parameter values of
min_hp to 1 and max_hp to 255. The data
parameter will be
passed the dataset (the global data
). For example,
filter_hp(data, 20, 25)
would return
20.0, 'Duskull'),
[(20.0, 'Feebas'),
(20.0, 'Magikarp'),
(20.0, 'Mime Jr.'),
(20.0, 'Pichu'),
(20.0, 'Shuckle'),
(25.0, 'Abra'),
(25.0, 'Blipbug'),
(25.0, 'Magnemite'),
(25.0, 'Wimpod')] (
Write code to create a list of dictionaries
gen_atk_def
, each of which stores a generation and the
median attack and defense values for that generation. Each dictionary
should be of the form
{'generation': <int>, 'attack': <float>, 'defense': <float>}
.
Recall the median is the middle value. For a sorted list of values [1,
3, 14, 17, 21], the median is 14; for [1, 3, 14, 17, 13, 21], it is the
average of the two middle values 14 and 17 = 15.5. For example, the
entry in your result for generation 1 should be
{'generation': 1, 'attack': 75.0, 'defense': 66.0}
.
//
may be useful.