The Art of Being "Lazy": Learning Text Compression with Boxes

不插電文字壓縮教案:用畫框框、指回前文的遊戲,讓孩子理解 ZIP 和 PNG 背後的壓縮原理。從兒歌歌詞開始動手玩,附逐步引導與延伸挑戰,適合國小與親子。

If you were punished and had to write "I will not talk in class" 100 times, would your hand get tired? Would you wish for a magic spell where you could write it just once and use arrows to point to the rest?

Congratulations! You think exactly like a computer scientist.

Computers have limited hard drive space, and sending data over the internet takes time. To "save space" and "save time," computers invented a magic trick to shorten text, which we call "Compression." Today, we don't need a computer; we only need paper and a pen to crack this code.

Phase 1: The Need for Speed (Discovering the Pain Point)

  • 聽寫挑戰:準備一首重複性很高的兒歌(例如原教材中的《The Rain》或《Baby Shark》)。
  • Teacher: "I am going to read a poem. Please write down every single word. Let's see who can write the fastest."
  • Read: Read deliberately fast. "Pitter patter, pitter patter, listen to the rain..."
  • Observe: The child will complain about being too slow or their hand hurting.

Constructive Inquiry:

  • Teacher: "Did you notice anything particular about this poem?"
  • Child: "Yes! It keeps repeating 'Pitter patter'!"
  • Teacher: "If you were a super smart scribe, could you find a way to write fewer words, but still make sure people can read the poem perfectly?"
  • (Listen to their creativity: They might write "x2" or draw arrows.)

Phase 2: The "Box and Pointer" Method (Inventing Symbols)

Introduce the concept of "Pointers," describing them as a "labor-saving tool."

1. Demonstration:

  • Teacher:「你的箭頭畫得很棒!電腦也用類似的方法。當一個字第二次出現時,它不再重寫,改成畫一個框框,用箭頭指回前面第一次出現的地方,說:『嘿,跟那裡一樣!』」

2. Practical Exercise:

  • Teacher"Look at this picture. Some words are missing, and only the road signs (arrows) remain. You are the decoder. Follow the arrows back to find the original words and fill them in."
  • Observation Points:Guiding Reflection:
    • Does the child notice the arrow points to "how many words"?
    • Does the child understand what the start and end of the arrow represent?

Phase 3: Pushing the Limits (The "Banana" Problem)

The example of turning Ban(2,3) into Banana is the most brain-twisting yet essential part of this activity.

In Computer Science, this is known as "Self-reference in the LZ77 Algorithm."

Children (and adults) often get stuck here:

"The computer tells me to count back 2 spaces and copy 3 letters. But looking back, there are only 2 letters ('a' and 'n'). The 3rd letter hasn't been written yet. How can I copy it?"

To teach this constructively, we must shift the concept from "Paste all at once" to a "One-by-one dynamic process."

1. Core Concept: Not "Copy-Paste," but a "Copycat Robot"

Traditional teaching treats (2,3) as a static instruction. In reality, it is a dynamic loop.

  • The first number (2): How far back the "Copycat Robot" looks (Distance).
  • The second number (3): How many steps the robot walks (Length/Repetition).
2. Step-by-Step Visualization

Let's take a strip of paper, write B, a, n, and draw three blanks _ _ _ after it.
Instruction:Look back 2 spaces, copy 3 times.。

Step 1
  • Current State:B a n [ ] _ _
  • Action: The cursor looks back 2 spaces.
    • n (1 space back) ← a (2 spaces back)
    • The robot sees a
  • Write: Put a in the first blank.
  • Result:B a n [a] _ _
Step 2
  • Current State:B a n a [ ] _
  • Key Point: The robot still follows the rule "Look back 2 spaces."
  • Action: The cursor looks back 2 spaces.
    • a (1 space back) ← n (2 spaces back)
    • The robot sees n。
  • WritePut n in the blank.
  • Result:B a n a [n] _
Step 3 — The Magic Moment
  • Current State:B a n a n [ ]
  • Action: Robot continues to "Look back 2 spaces."
    • n (1 space back) ← a (2 spaces back)
    • Notice! Who is this a? It is the same a we just wrote in Step 1!
    • We are copying "something new that was just created."
  • Write: Put a in the first blank.
  • Result:B a n a n [a]
Try it

COMMAND_MODULE

MEMORY_BUFFER ADDR: 0x0000 – 0x0005

SYSTEM_LOG
3. The Insight: What does this mean?

When Copy Length > Look-back Distance, a "Self-plagiarism" phenomenon occurs.

This represents a Repeating Pattern.

  • Ban(2, 3) means: Take the last 2 letters an and use them as a stamp, stamping continuously until you fill 3 spots.
  • What if it's Ban(2, 10) ?
    • It becomes B + an an an an an... = Bananananana...

"The key to teaching the 'Banana' example lies in breaking the misconception of 'Static Copying' (or 'Static Copy-Paste').

請使用「時間軸」或「慢動作」的概念,讓孩子親手一個字、一個字地寫出來。當他們發現第三個字 a 是來自於他們第一步寫下的 a 時,他們就懂了 LZ 壓縮演算法中最精妙的「重疊複製」技巧了(電腦通常還會限制只能往回指到最近的一段範圍,稱為「滑動視窗(Sliding Window)」)。

Phase 4: Connecting to Reality (Why does it matter?)

Teacher: "Do you know why we go through all this trouble?"

  • Teacher: "Imagine you have to pack an entire library of books into one suitcase. If the books contain many repeated sentences, and we use this 'arrow method,' will the books become thinner or thicker?"
  • Summary:「這就是為什麼你可以很快地下載遊戲、打開壓縮檔。因為聰明的電腦先把重複的資料『壓縮』了,傳到你家再『解壓縮』還原回來。這就是 ZIP 檔和 PNG 圖片的原理!(影片用的是另一種會丟掉部分細節的壓縮法,那是另一個故事了。)」

Teaching Observation Checklist

Observation PointLess Effective ReactionMore Effective Scaffolding
When the child doesn't know where the arrow points"Look, the arrow points to P, so you write P." (Giving the answer)"Try walking your finger along this line. Which word does it land on effectively? Who is that word?"
Handling the "Banana" Problem (Overlapping Match)"The formula is (2,3): Back 2, Length 3.""Let's pretend to be a robot. The only instruction is: 'Copy the letter that is 2 steps behind me.' Let's do this action 3 times and see what happens."
Discussing Efficiency"The compression ratio formula is...""Which kind of book do you think is easiest to compress? A dictionary? Or a book that just says 'Blah blah blah' over and over again?" (Guiding to redundancy)

💡 Advice for Parents/Teachers

The core of this article is "Visualization." Don't rush into numbers (like the Ban(2,3) challenge). First, get the child used to drawing boxes and pulling lines.

When a child can skillfully turn Twinkle Twinkle Little Star into a messy drawing full of arrows, they have mastered one of the core technologies of modern internet transmission: LZ Coding (Ziv-Lempel Coding).

Tell the child: "Next time you are punished to write lines, try writing the first line and then using arrows for the rest. See if your teacher admires your 'Computational Thinking'! (But you might get into trouble, so do it at your own risk XD)."

Reference :csunplugged text_compression

延伸閱讀