To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Lab 5c — Counting
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 lab5c
.
Practice
10 minutes
We have given you code in practice.py
that provides some simple practice with
counting. This program counts the unique words you enter. You will see some
comments telling you to do three things:
-
Check if the word is in the counts dictionary
-
If it is not:
- Initialize the count for that word to zero
- For each word, increment the count for that word by one
After you finish and run the program, it should print:
Word: hello
Word: hello
Word: hello
Word: goodbye
Word:
{'hello': 3, 'goodbye': 1}
Discuss with the TA:
- Show a solution.
- Is there anything you don’t understand about counting with dictionaries?
Better writing
15 minutes
We have given you some code in better_writing.py
that is supposed to count how
many times a file contains the words “very” and “really”. You need to write the
count_words()
function:
def count_words(content: str, words: list[str]) -> dict[str, int]:
"""
content -- a string
words -- a list of strings
Count how many times each word in <words> appears in <content>.
Return a dictionary that maps each word in <words> to its count.
"""
# Write code here
pass
We have given you a file called writing.txt
that contains an example of bad
writing:
I was going really fast down a very steep hill. In fact, I was going so
fast my tires were a blur and I was really scared. Suddenly I saw a very
small dog crossing my path. I pushed the brakes really hard and stopped
just in time. I was very grateful and really glad.
After you write this code, run the program with:
python better_writing.py writing.txt
You should see something like this:
very: 3
really: 4
Tips
-
You are counting only the words we have given you, not all the words. This means you can initialize those counts to zero before looping through the words.
-
We have given you a function called
readfile()
that reads the entire file and returns it as one big string. This is different than thereadlines()
function we have used, which reads the file and returns a list of lines.
Discuss with the TA:
- How did you implement this function? Show a solution and discuss.
- Is there anything you don’t understand about dictionaries?
- Make sure everyone understands every line of code we supplied, including the input loop.
Counting Pokemon
15 minutes
We have given you some code in pokemon_count.py
that is supposed to go through
a CSV file that contains information about Pokemon and count all the Pokemon of
each type, e.g. “fire” and “water”. You do not know in advance all of the types
of Pokemon. To complete this code, you need to write this function:
def count_pokemon(lines: list[str]) -> dict[str, int]:
"""
lines: a list of lines, each line is a comma-separated list of values
Count all of the Pokemon of each type. The type is in column 4
of the CSV.
Return a dictionary that maps type to its count.
"""
# Write code here
pass
We have given you a file called Pokemon.csv
that contains a CSV with a lot of
data on 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
We are only concerned about type.
After you write the code, run the program with:
python pokemon_count.py Pokemon.csv
You should see something like this:
grass: 86
fire: 58
water: 123
bug: 75
...
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()
, the type of the Pokemon is item 4 in the list, since we index lists starting from zero.
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. 😊