To start this assignment, download this zip file.
Lab 5d — Grouping
Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab5d
.
Group by first two letters
15 minutes
We have given you code in group_by_first_two_letters.py
that is supposed to
group words you type in based on their first two letters. This is very similar
to the code in the guide on grouping. You need to write
the group_by_first_two_letters()
function:
def group_by_first_two_letters(words: list[str]) -> dict[tuple[str, str], list[str]]:
"""
Group a list of words by their first two letters.
words -> a list of strings
returns a dictionary that maps a letter to a list of words
"""
# Write code here
pass
After you write this code and run it, it should see something like this:
Word: horse
Word: cow
Word: hope
Word: cool
Word: weird
Word:
['horse', 'cow', 'hope', 'cool', 'weird']
{('h', 'o'): ['horse', 'hope'], ('c', 'o'): ['cow', 'cool'], ('w', 'e'): ['weird']}
- How did you implement this function? Show a solution and discuss.
- Is there anything you don’t understand about grouping?
- Make sure everyone understands every line of code we supplied.
Grouping Pokemon
20 minutes
You may remember that the previous lab had you count Pokemon of the same type (e.g. “fire” and “water”). For this lab, we are going to do a similar thing, but group the Pokemon by their attributes instead of counting them.
We have given you some code in pokemon_group.py
that is supposed to go through
a CSV file that contains information about Pokemon and group all the Pokemon
using whatever column you want.
To complete this code, you need to write this function:
def group_pokemon(lines: list[str], column: int) -> dict[str, list[str]]:
"""
lines: a list of lines, each line is a comma-separated list of values
column: an integer column number
Find all of the Pokemon who have the same value in the given column.
Return a dictionary that maps the column value to the list of Pokemon.
For example, if the column is the number 4, then the dictionary will
map the Pokemon type (water, fire) to a list of Pokemon of that type.
"""
# Write code here
pass
Notice that we are giving you a column here. You can’t assume the column is always 4 (the type of Pokemon). It could be any other column.
We have given you a file called Pokemon-small.csv
that contains a CSV with a
just a few of the Pokemon. The full information it has is:
- name
- pokedex id (a unique number)
- height
- weight
- type
- secondary type
- HP
- attack
- defense
- special attack
- special defense
- speed
After you write the code, run the program with:
python pokemon_group.py Pokemon-small.csv 4
you should see something like this, which groups Pokemon by their type:
grass
* bulbasaur
* ivysaur
* venusaur
fire
* charmander
* charmeleon
* charizard
water
* squirtle
* wartortle
* blastoise
bug
* caterpie
* metapod
* butterfree
If you run it with:
python pokemon_group.py Pokemon-small.csv 6
you should see something like this, which groups Pokemon by their HP:
45
* bulbasaur
* caterpie
60
* ivysaur
* butterfree
80
* venusaur
39
* charmander
58
* charmeleon
78
* charizard
44
* squirtle
59
* wartortle
79
* blastoise
50
* metapod
Tips
- Since you are reading a CSV file, you need to handle it line by line. As you
loop through the lines you want to use
strip()
to remove any trailing newline characters and thensplit(',')
to split on commas so you can look at each column:
tokens = line.strip().split(',')
- After you
split()
, you now have tokens. Thecolumn
variable tells you which column to use for your key. Then you want to put the name of the Pokemon into a list. The name is in column0
.
Grading
To finish this lab and receive a grade, take the canvas quiz.
Solution
We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)