Line-by-Line Diff

hard
Unit 24 · Validation0/1 to milestone

Implement a simple version of diff. Given two lists of lines, produce an edit script based on their longest common subsequence:

  • diff_lines(a, b): a list of strings, one per line: " line" for unchanged lines, "- line" for removed lines and "+ line" for added lines. When there's a choice, output removals before additions at each change point.
  • apply_diff(a, script): rebuild b from a and the script, checking that unchanged and removed lines match a (raise ValueError otherwise).
  • diff_stats(script): {"added": n, "removed": n, "unchanged": n}.
Pro problem

Unlock this and every premium problem across all tracks — plus hidden-test grading, advanced AI engineering modules and new content every week.

Start 7-day free trial
solution.py · Python 3.13
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Line-by-Line Diff · TechGradify