Python Plagiarism Check
import re
def plagiarism_check(code1: list[str], code2: list[str]) -> bool:
c1 = " ".join(code1)
c2 = " ".join(code2)
if c1 == c2:
return False
d1 = re.findall(r"\w+", c1)
d2 = re.findall(r"\w+", c2)
r_cand: dict[str, str] = {}
for v, w in zip(d1, d2):
if v != w and not v.isdigit():
r_cand[v] = w
for orig, _repl in r_cand.items():
c1 = re.sub(r"(\W)" + re.escape(orig) + r"(\W*)", r"\1PLACEHOLDER" + orig + r"\2", c1)
c1 = re.sub(r"(\W)" + re.escape(orig), r"\1PLACEHOLDER" + orig, c1)
for orig, repl in r_cand.items():
c1 = re.sub(r"(\W)PLACEHOLDER" + re.escape(orig) + r"(\W)", r"\1" + repl + r"\2", c1)
c1 = re.sub(r"(\W)PLACEHOLDER" + re.escape(orig), r"\1" + repl, c1)
return c1 == c2
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.