BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Homework 5d — Grouping

1. Blue score

Write a program in that reads phrases from input and then groups those phrases by their “blue score”. You can calculate a “blue score” for a phrase by counting how many times it contains one of these words: 'byu', 'cougar', 'cougars', 'blue'. Ignore case and punctuation. For example, here are some phrases and their scores:

WordBlue Score
I saw a blue bird at BYU.2
The blue cougar was feeling blue.3
BYU is great. Go cougars!2
What does the cougar say?1

Write your program so the output matches what is shown below:

Phrase: I saw a blue bird at BYU.
Phrase: The blue cougar was feeling blue.
Phrase: BYU is great. Go cougars!
Phrase: What does the cougar say?
I saw a blue bird at BYU.
BYU is great. Go cougars!

The blue cougar was feeling blue.

What does the cougar say?

Note that there is an extra blank line after each group of phrases.


To remove punctuation from a word you can do this:

from string import punctuation

word = word.strip(punctuation)

2. Word groups

Write a program in that takes

  • a file name

The program should read all of the words in the file and then group them by their length and their first letter, as a tuple (length, letter).

For example, the file some_words.txt contains:

Apple, ash, aspen.
Pears and plums, pretty please?
Where what whom!

If you run the program, it should produce this:

% python words.txt
(5, 'a'): ['apple', 'aspen']
(3, 'a'): ['ash', 'and']
(5, 'p'): ['pears', 'plums']
(6, 'p'): ['pretty', 'please']
(5, 'w'): ['where']
(4, 'w'): ['what', 'whom']


To get a list of all the words in a file, given the filename, use this function:

def readwords(filename):
    Read a file given by the filename and
    return a list of all of the words. The words
    are separated based on whitespace.
    Note, punctuation remains with the words!
    with open(filename) as file:

To convert a word to lowercase and remove punctuation, recall that we can do this for a word:

from string import punctuation

word = word.lower().strip(punctuation)

3. Group CSV

In this problem, you are going to write code that can group columns from a CSV file. Write your code in This program takes three arguments:

  • an input file name
  • a column to group by (column A)
  • a column of values (column B)

The program should group all of the values in column B based on the keys in column A. For example, we have given you a CSV file called grades.csv:


If you run the program with:

python grades.csv 1 0

then the program will group all of the students by their grades on the 1st assignment (i.e. the 2nd column; remember we count columns starting at zero!)

This should print:

A: ['Emma']
B: ['Harold', 'Sarah']
C+: ['McKayla']
A-: ['Jose']
B+: ['Jenna']
F: ['Joshua']

You can also run the program to group the students based on their grades on the 2nd assignment:

python grades.csv 2 0

This should print:

A: ['Emma']
A-: ['Harold']
B-: ['McKayla']
B+: ['Jose']
B: ['Jenna', 'Sarah']
A+: ['Joshua']

This is very similar to a problem from the lab, except we are telling you the column for the key and the column for the value.


Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.


Blue score5
Word groups7
Group CSV8

Manual Grading

Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.


You should not use a csv reader for this assignment. They are useful, but so is understanding how to strip and split a line yourself.

Remember not to use system arguments outside of if __name__ == "__main__":.

Printing a dictionary to look like {"key" : "value" }, in the order that keys were added, is an easy task. Simply use print(name_of_dictionary).

This is a good point in the class to reread the academic integrity section of the syllabus. AI tools should not be used to complete your assignments.