Problem 217493 · medium · Level 02 Linear Data Structures

The First Line That Changed

py-zip · py-enumerate · strings

Two versions of a text file are given as lists of lines, old and new. Write first_change(old, new) that reports the first line where they differ, as a tuple (line_number, old_line, new_line):

  • line numbers start at 1;
  • if one version simply ends earlier, the first difference is the line just after its end, and the missing line is reported as None;
  • if the versions are identical, return None.

Examples

Input:  old = ["def f(x):", "    return x", ""], new = ["def f(x):", "    return x + 1", ""]
Output: (2, "    return x", "    return x + 1")

Input:  old = ["a", "b"], new = ["a", "b", "c"]
Output: (3, None, "c")

Input:  old = ["same"], new = ["same"]
Output: None

Constraints

  • 0 <= len(old), len(new) <= 10**5
  • Lines are compared exactly, including spaces.

Goals

  • Walk two sequences side by side with `zip`
  • Number the pairs with `enumerate(..., start=1)`
  • Handle the part that `zip` leaves out when the lengths differ
Starting Python…