Rust Plagiarism Check
use std::collections::HashMap;
fn tokenize(s: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut current = String::new();
for c in s.chars() {
if c.is_alphanumeric() || c == '_' {
current.push(c);
} else if !current.is_empty() {
tokens.push(std::mem::take(&mut current));
}
}
if !current.is_empty() {
tokens.push(current);
}
tokens
}
fn is_numeric_token(s: &str) -> bool {
s.parse::<f64>().is_ok()
}
fn plagiarism_check(code1: &[String], code2: &[String]) -> bool {
let c1 = code1.join(" ");
let c2 = code2.join(" ");
if c1 == c2 {
return false;
}
let d1 = tokenize(&c1);
let d2 = tokenize(&c2);
let mut r_cand: HashMap<String, String> = HashMap::new();
for (v, w) in d1.iter().zip(d2.iter()) {
if v != w && !is_numeric_token(v) {
r_cand.insert(v.clone(), w.clone());
}
}
let renamed: Vec<String> = d1
.iter()
.map(|t| r_cand.get(t).cloned().unwrap_or_else(|| t.clone()))
.collect();
renamed == d2
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.