Skip to content

Week 8: strings, dictionaries, and testing

In Week 6 your loops walked through lists, one item at a time. This week they walk through strings, one character at a time, because a string is a sequence too. You also meet the dictionary, which stores a value under a key, so you can keep one count for every letter at once instead of one variable per letter. And you stop checking your functions by eye: a pytest test writes down the answer you expect, and pytest tells you whether your function got it.

This week's practice is your second programming assignment (PA2), and it has the same shape as PA1: one program, in Parts A, B, and C. What's new is Part C. Instead of a third function, it's a test file you write yourself, so you turn in two files. There are two options, and you pick one. They are the same size and practice the same skills, so go with the story you like better.

Save your PA in the Week08 folder inside ~/CS149, and commit and push your work when you finish. Each option has an assignment on Gradescope, and you can submit as many times as you like, so check your work as you go.

Submit early to Gradescope

If you don't submit to Gradescope and get a nonzero score, you will receive a reflection grade of no higher than 15/30 points.

Loops are still for loops

while is next week, and neither option needs it. The same goes for anything that does a loop's job for you, like count(), find(), slicing, or a dictionary's get(): Gradescope rejects them, because writing the loop yourself is the point of the week. String methods that test one character, like isalpha(), are fine.

Option 1: Hot Letters 🔥

Green is on fire {s}, yellow is warm [s], gray is ice cold (s). It's the scoring rule from Wordle, and you're the one who hands out the colors. But what color is the second e in a guess when the secret word has only one? Your code settles it: it counts the letters not matched yet, then colors each letter of the guess, using up a letter each time it awards a yellow.

Start Hot Letters →

Option 2: Magazine Collage ✂️

Grab an old magazine, a pair of scissors, and a glue stick, and make a card nobody else could make: every letter snipped from a different headline. Your code is the scissors. It counts the letters on a page, then pastes up a message from them, one letter at a time, leaving a gap wherever the page has run out.

Start Magazine Collage →

What's the same in both

Each option is two files: a module with two functions, one per part, and a test file for Part C. The main block that runs the finished program is written for you.

Part Hot Letters Magazine Collage What you practice
A leftovers letter_counts looping through a string to count its letters in a dictionary
B score cut_out building a new string, letter by letter, using up those counts
C test_hot.py test_collage.py writing pytest tests that catch mistakes, including edge cases

Start with Part C

Part C is listed last, but it's the best place to begin, and you can start it right after the pytest class, before dictionaries come up. A test needs only a call and the answer you expect, and you can work out the answer by hand from the docstring and your option's page. So write a few tests for leftovers or letter_counts first, then write the function until they pass, and submit to Gradescope before you start Part B.

Upload both files, every time

Gradescope needs both files in every submission, or it gives you 0 points. If you haven't started your tests yet, upload the test file just as you downloaded it; your functions still get graded.

When you're done

Commit and push your Week08 folder:

git add -A
git commit -m "Add week 8 PA"
git push

Then pick one test from your Part C and trace your Part B function on it by hand: write down the dictionary after every letter, and the string you are building. If your trace and your test agree, you understand your own loop, and not just that it passes.