← Back to course
Intro to CS 9-10 / Week 10 / Wednesday
3/6
Week 10 Β· Data, Compression and Models

Wednesday

Beacon Lab: Shrink a photo
// Shrinking photos, cleaning logs and simulating growth
⏱ about 20 min

Wednesday: Beacon Lab: Shrink a Photo

The plant team wants tonight's photo of the lettuce bed, but the relay can only carry a short message.

Wren lays out a paper grid of the photo. Each square is a letter: G for a green leaf and W for the white wall.

"Row one is G, G, G, G, W, W, W, G," Comet reads. "That is eight letters for one row."

"What do you notice about those letters?" Wren asks.

Comet grins. "They come in runs. Four G, then three W, then one G."

Nova blinks approvingly. "Then send the runs, not the letters," she says. "And check that the team can rebuild the exact row."

Comet grabs a pencil. "Let's build it."

Your mission

You will compress a paper photo with a run-length code. This is the crew's classroom code for the lab.

A run is a group of the same letter in a row. The code writes how long each run is, then the letter.

So G G G G W W W G becomes 4G 3W 1G. That is 6 symbols instead of 8.

  • Graph paper, or a ruler and plain paper
  • A pencil and an eraser
  • A green pencil or crayon (optional)
  • A partner to decode your message (optional)
  1. Draw an 8 by 4 grid. Copy the lettuce photo from the table below, one letter per square.
  2. Shade every G square green or gray. Leave every W square blank.
  3. For each row, write the run-length code, such as 4G 3W 1G.
  4. Count the symbols in each code. A number counts as one symbol and a letter counts as one symbol.
  5. Give only your codes to a partner. Ask them to rebuild the grid on fresh paper. No partner? Cover your grid and rebuild it yourself from the codes.
  6. Compare the two grids square by square.
Lettuce photoSquares 1 to 8
Row 1G G G G W W W G
Row 2G G G G G G G G
Row 3W W W W W W W W
Row 4G W G W G W G W
PREDICT BEFORE YOU CODE
  • Read the question.
  • Tap your answer.
What is the run-length code for row 2?
Which row shrinks the least?
Row 4 codes as 1G 1W 1G 1W 1G 1W 1G 1W. How many symbols is that?
How many symbols is the code for row 1, 4G 3W 1G?

Row 4 got longer, not shorter. Compression depends on redundancy, and row 4 has none.

This code is lossless. If your partner followed the codes, their grid matches yours exactly.

Now make it lossy

The relay is even slower tonight. The crew tries a lossy shortcut of its own.

Rule: if a run is only one square long, paint it the same letter as the run before it.

So row 1, 4G 3W 1G, becomes 4G 4W. That is 4 symbols, but the last leaf is gone.

What is the run-length code for row 3?
After the lossy rule, what does row 4 become?
What the lab showsTrue or false?
The run-length code rebuilds the exact grid.?
The lossy shortcut rebuilds the exact grid.?
A row with no repetition can get longer when coded.?
The lossy shortcut lost the single leaf at the end of row 1.?
WHY THIS EXERCISELossless keeps every square; lossy trades detail for size.
Row 2 codes as 8G. Is that code lossless or lossy? Type one word.
WHY THIS EXERCISEA code that rebuilds every square exactly is lossless, no matter how short it is.
On paper, sketch the lettuce grid twice: once rebuilt from the lossless codes, once rebuilt after the lossy rule. Circle every square that changed.
Try it
Invent your own 8 by 4 grid that shrinks to fewer than 12 symbols in total with the run-length code.
Then invent one that grows longer than 32 symbols. What makes the difference?

Great lab work. Tomorrow you will grow a whole tomato plant inside a simulation.

← Tuesday