Fix the line matcher in a license scanner

Review the AI's codeEasy10 minMicrosoft and Anthropic style

Your task

A license audit found that an AI-generated rope library in your closed-source editor was nearly a copy of a GPL-3.0 project. GPL code inside a closed product can oblige you to publish your source. Your team now runs a similarity check in CI that scores generated code against a corpus of open-source code.

An assistant wrote provenance_match_percent(generated_lines, reference_lines). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • generated_lines: the lines of the generated file.
  • reference_lines: the lines of the open-source corpus.
  • Return the percent of meaningful generated lines found in the corpus, as an integer.

Rules the tests check:

  • Normalize every line, generated and reference: drop spaces, tabs and \r at both ends, and turn every run of spaces, tabs and \r inside the line into one space. Nothing else changes, and case matters.
  • After normalizing, a generated line is noise if it is empty, {, } or };. Skip noise.
  • Count each distinct generated line once, however often it repeats.
  • A distinct line matches when it equals some normalized reference line.
  • Return the matched count times 100, divided by the distinct count, rounded down. If there are no distinct lines, return 0.

Examples

provenance_match_percent(generated_lines: list[str], reference_lines: list[str]) -> int
Example 1
Inputprovenance_match_percent(["func (r *Rope) Insert(idx int, s string) {", " if idx < 0 || idx > r.length {", " return", " }", " r.root = insertNode(r.root, idx, s)", "}"], ["func (r *Rope) Insert(idx int, s string) {", " if idx < 0 || idx > r.length {", " return", " }", " r.root = insertNode(r.root, idx, s)", "}"])
Output100
Example 2
Inputprovenance_match_percent(["package rope", "", "type Rope struct {", " root *node", "}"], ["package tree", "", "type Tree struct {", " root *node", "}"])
Output33
Example 3
Inputprovenance_match_percent([], ["anything"])
Output0