Skip to the workspace
Pebellslearn by building
Sequence DP: the Diff Engine  ›  Capstone Project

Capstone: Quarry's Diff Engine

50 minTurn the LCS table over two revisions into a readable diff with change counts

dsa / Capstone: Quarry's Diff Engine

Explore each scene at your own pace.

01 · What the engine returns

What the engine returns

Capstone — Quarry's Diff Engine

Definition
A diff engine compares two versions of a text and lists which lines were kept, removed or added. It splits both into lines, fills a longest-common-subsequence table over those lines, then walks back through the table to produce the changes in order.
In plain words
It is the changes view you see when two drafts of a document are put side by side: untouched lines stay plain, deleted lines are marked with a minus and new lines with a plus.

In short: a line-by-line diff is the LCS table from lesson 2, run over whole lines instead of letters, with the walk-back from lesson 4 turning the table into kept/removed/inserted output lines in file order. Splitting text into lines is part of the algorithm — splitlines() and split("\n") disagree about a trailing newline, and that difference shows up as a phantom diff line if you're not careful.

A diff hunk with a header and six lines: one kept, one removed in red, one inserted in green, one kept, one inserted in green, one kept

Quarry now has four pieces of version history. The reorderer knows which blocks can stay put. The survival score says how much of a document lived through an edit. The typo checker measures how far apart two strings are. The edit script turns a table into keep, delete, insert. What Quarry still can't do is the thing a person actually clicks on: show me what changed between these two revisions, as text, line by line.

That's a diff. You'll build it from the table you already know, run it on real documents, and then, in the exercises, apply it, reverse it, colour it word by word and cut it into hunks.

What the engine returns

Two revisions come in as whole strings. The engine:

  1. splits each into lines,
  2. fills the LCS table over the lines,
  3. walks back from the corner with the tie rule from lesson 4 (a match goes diagonal; otherwise left when table[i][j-1] >= table[i-1][j], else up),
  4. writes each step as one line of output: " " + line for a kept line, "-" + line for a deleted one and "+" + line for an inserted one,
  5. and counts them.

Here's the table for a small settings file. Old version down the side, new version across the top:

watch it move · grid

The same LCS table as lesson 2, over whole lines. The path back from the corner is the diff.

Row 0 and column 0 compare against an empty file. 5 by 6 table. At row 0, column 0 — value 0. 10 of 30 cells filled.

1 / 6

Reversed, that walk is the diff:

 [store]
-shards=2
+shards=4
 cache=on
+backup=daily
 log=warn
python

For m and n lines, filling the table is O(m · n) time, one constant-work comparison per cell, and keeping it costs O(m · n) space — the walk back afterwards only adds O(m + n) on top.

Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — version history
This piece — diff_versions(): Diffs two stored revisions into readable output with change counts.
Scenario Quarry's history panel compares any two revisions of a stored document and shows exactly what changed, line by line, with counts for the summary.
diff_versions.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
diff_versions(old, new) → dictThe line-by-line diff between two revisions, with counts.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.