Goals

The goal of this assignment is to work on lists and dictionaries in Python.

Instructions

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 Google Colab) or on your own local installation of Jupyter and Python. You should use Python 3.8 or higher for your work. To use tiger, use the credentials you received. For Colab, you will need to use a Google account. 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 name
  • generation: 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 points
  • height_m: height in meters
  • weight_kg: weight in kilograms
  • speed: base speed
  • attack: base attack
  • defense: base defense

You 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.

Due Date

The assignment is due at 11:59pm on Thursday, February 11 Friday, February 12.

Submission

You should submit the completed notebook file required for this assignment on Blackboard. The filename of the notebook should be a3.ipynb.

Details

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.

0. Name & Z-ID (5 pts)

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.

1. Minimum Hit Points (10 pts)

Write code to find the Pokemon with the minimum number of base hit points (hp). Output its name. 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.

2. Generation Counts (10 pts)

Write code to create a new dictionary generation_counts that stores the counts of each generation in the dataset.

3. Mean Weights by Primary Type (15 pts)

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.

Hints
  • Consider creating a dictionary mapping primary type to a list of weights first, and then compute the mean from each list.
  • Some pokemon have unknown weights. In this case, weight_kg is set to None. Make sure to exclude them from mean computations.

4. Filter by Hit Points (15 pts)

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')]
Hints
  • Remember that we can sort values of a list in two ways.
  • Tuples will be sorted by first comparing their first element, then second element, and so on.

5. Median Attack & Defense by Generation (20 pts)

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, 4, 7, 21], the median is 4; for [1, 3, 4, 7, 13, 21], it is the average of the two middle values 4 and 7 = 5.5. For example, the entry in your result for generation 1 should be {'generation': 1, 'attack': 75.0, 'defense': 66.0}.

Hints
  • The need to average the middle values is determined by whether the list has even or odd length
  • Make sure you sort the list before accessing the middle values by index.
  • Think about how that index calculation should work; // may be useful.