Go Plagiarism Check
func plagiarismCheck(code1, code2 []string) bool {
c1 := strings.Join(code1, " ")
c2 := strings.Join(code2, " ")
if c1 == c2 {
return false
}
wordRe := regexp.MustCompile(`[\w]+`)
numericRe := regexp.MustCompile(`^[0-9]+$`)
d1 := wordRe.FindAllString(c1, -1)
d2 := wordRe.FindAllString(c2, -1)
rCand := make(map[string]string)
var order []string
for k, v := range d1 {
if k >= len(d2) {
break
}
if v != d2[k] && !numericRe.MatchString(v) {
if _, exists := rCand[v]; !exists {
order = append(order, v)
}
rCand[v] = d2[k]
}
}
// Two-pass rename: stash originals behind a placeholder first so that
// swapping two identifiers (e.g. a<->b) doesn't clobber itself, then
// swap the placeholders in for the real replacements.
for _, orig := range order {
re1 := regexp.MustCompile(`(\W)` + regexp.QuoteMeta(orig) + `(\W*)`)
c1 = re1.ReplaceAllString(c1, "${1}PLACEHOLDER"+orig+"$2")
re2 := regexp.MustCompile(`(\W)` + regexp.QuoteMeta(orig))
c1 = re2.ReplaceAllString(c1, "${1}PLACEHOLDER"+orig)
}
for _, orig := range order {
repl := rCand[orig]
re3 := regexp.MustCompile(`(\W)PLACEHOLDER` + regexp.QuoteMeta(orig) + `(\W)`)
c1 = re3.ReplaceAllString(c1, "${1}"+repl+"$2")
re4 := regexp.MustCompile(`(\W)PLACEHOLDER` + regexp.QuoteMeta(orig))
c1 = re4.ReplaceAllString(c1, "${1}"+repl)
}
return c1 == c2
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Haskell Plagiarism Check
import Data.Char (isAlphaNum, isDigit)
import qualified Data.Map.Strict as Map
data Token = Word String | Sep String
tokenize :: String -> [Token]
tokenize [] = []
tokenize s@(c : _)
| isWordChar c = let (w, rest) = span isWordChar s in Word w : tokenize rest
| otherwise = let (sp, rest) = break isWordChar s in Sep sp : tokenize rest
where
isWordChar ch = isAlphaNum ch || ch == '_'
plagiarismCheck :: [String] -> [String] -> Bool
plagiarismCheck code1 code2
| c1 == c2 = False
| length words1 /= length words2 = False
| otherwise = reconstructed == c2
where
c1 = unwords code1
c2 = unwords code2
toks1 = tokenize c1
words1 = [w | Word w <- toks1]
words2 = [w | Word w <- tokenize c2]
isNumeric w = not (null w) && all isDigit w
-- later mismatches overwrite earlier ones for the same original token,
-- mirroring the source's plain array assignment
rCand =
Map.fromList
[(w1, w2) | (w1, w2) <- zip words1 words2, w1 /= w2, not (isNumeric w1)]
substitute (Word w) = Map.findWithDefault w w rCand
substitute (Sep s) = s
reconstructed = concatMap substitute toks1
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Java Plagiarism Check
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static boolean plagiarismCheck(String[] code1, String[] code2) {
String c1 = String.join(" ", code1);
String c2 = String.join(" ", code2);
if (c1.equals(c2)) {
return false;
}
List<String> d1 = new ArrayList<>();
List<String> d2 = new ArrayList<>();
Matcher m1 = Pattern.compile("\\w+").matcher(c1);
while (m1.find()) {
d1.add(m1.group());
}
Matcher m2 = Pattern.compile("\\w+").matcher(c2);
while (m2.find()) {
d2.add(m2.group());
}
Map<String, String> rCand = new LinkedHashMap<>();
for (int k = 0; k < d1.size(); k++) {
String v = d1.get(k);
if (!v.equals(d2.get(k)) && !v.matches("\\d+")) {
rCand.put(v, d2.get(k));
}
}
for (Map.Entry<String, String> e : rCand.entrySet()) {
String orig = e.getKey();
c1 = c1.replaceAll("(\\W)" + orig + "(\\W*)", "$1PLACEHOLDER" + orig + "$2");
c1 = c1.replaceAll("(\\W)" + orig, "$1PLACEHOLDER" + orig);
}
for (Map.Entry<String, String> e : rCand.entrySet()) {
String orig = e.getKey();
String repl = e.getValue();
c1 = c1.replaceAll("(\\W)PLACEHOLDER" + orig + "(\\W)", "$1" + repl + "$2");
c1 = c1.replaceAll("(\\W)PLACEHOLDER" + orig, "$1" + repl);
}
return c1.equals(c2);
}
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Lisp Plagiarism Check
(defun word-char-p (ch)
(or (alphanumericp ch) (char= ch #\_)))
(defun word-tokens (s)
(let ((tokens '())
(n (length s))
(i 0))
(loop while (< i n)
do (if (word-char-p (char s i))
(let ((start i))
(loop while (and (< i n) (word-char-p (char s i)))
do (incf i))
(push (subseq s start i) tokens))
(incf i)))
(nreverse tokens)))
(defun numeric-token-p (tok)
(and (> (length tok) 0) (every #'digit-char-p tok)))
(defun boundary-replace (str token replacement)
(let ((out (make-string-output-stream))
(tlen (length token))
(n (length str))
(i 0))
(loop while (< i n)
do (let ((pos (search token str :start2 i)))
(cond
((null pos)
(write-string str out :start i :end n)
(setf i n))
((and (> pos 0) (not (word-char-p (char str (1- pos)))))
(write-string str out :start i :end pos)
(write-string replacement out)
(setf i (+ pos tlen)))
(t
(write-string str out :start i :end (1+ pos))
(setf i (1+ pos))))))
(get-output-stream-string out)))
(defun plagiarism-check (code1 code2)
(let ((c1 (format nil "~{~A~^ ~}" code1))
(c2 (format nil "~{~A~^ ~}" code2)))
(if (string= c1 c2)
nil
(let ((d1 (word-tokens c1))
(d2 (word-tokens c2))
(r-cand (make-hash-table :test 'equal))
(r-order '()))
(loop for v in d1
for w in d2
do (when (and (not (string= v w)) (not (numeric-token-p v)))
(unless (nth-value 1 (gethash v r-cand))
(push v r-order))
(setf (gethash v r-cand) w)))
(setf r-order (nreverse r-order))
(dolist (orig r-order)
(setf c1 (boundary-replace c1 orig (concatenate 'string "PLACEHOLDER" orig))))
(dolist (orig r-order)
(setf c1 (boundary-replace c1 (concatenate 'string "PLACEHOLDER" orig) (gethash orig r-cand))))
(string= c1 c2)))))
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
PHP Plagiarism Check
function plagiarismCheck($code1, $code2)
{
$c1 = implode(" ", $code1);
$c2 = implode(" ", $code2);
if ($c1 === $c2) {
return false;
}
$d1 = $d2 = $rCand = [];
preg_match_all('/[\w]+/', $c1, $d1);
preg_match_all('/[\w]+/', $c2, $d2);
foreach ($d1[0] as $k => $v) {
if ($v !== $d2[0][$k] && ! is_numeric($v)) {
$rCand[$v] = $d2[0][$k];
}
}
foreach ($rCand as $orig => $repl) {
$c1 = preg_replace("/(\W){$orig}(\W*)/", '$1PLACEHOLDER' . $orig . '$2', $c1);
$c1 = preg_replace("/(\W){$orig}/", '$1PLACEHOLDER' . $orig, $c1);
}
foreach ($rCand as $orig => $repl) {
$c1 = preg_replace("/(\W)PLACEHOLDER{$orig}(\W)/", '$1' . $repl . '$2', $c1);
$c1 = preg_replace("/(\W)PLACEHOLDER{$orig}/", '$1' . $repl, $c1);
}
return $c1 === $c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
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.
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.
TypeScript Plagiarism Check
function isNumeric(value: string): boolean {
return value !== "" && !Number.isNaN(Number(value));
}
function plagiarismCheck(code1: string[], code2: string[]): boolean {
let c1 = code1.join(" ");
let c2 = code2.join(" ");
if (c1 === c2) {
return false;
}
const d1 = c1.match(/[\w]+/g) ?? [];
const d2 = c2.match(/[\w]+/g) ?? [];
const rCand = new Map<string, string>();
for (let k = 0; k < d1.length; k++) {
const v = d1[k];
if (v !== d2[k] && !isNumeric(v)) {
rCand.set(v, d2[k]);
}
}
for (const [orig] of rCand) {
c1 = c1.replace(new RegExp(`(\\W)${orig}(\\W*)`, "g"), `$1PLACEHOLDER${orig}$2`);
c1 = c1.replace(new RegExp(`(\\W)${orig}`, "g"), `$1PLACEHOLDER${orig}`);
}
for (const [orig, repl] of rCand) {
c1 = c1.replace(new RegExp(`(\\W)PLACEHOLDER${orig}(\\W)`, "g"), `$1${repl}$2`);
c1 = c1.replace(new RegExp(`(\\W)PLACEHOLDER${orig}`, "g"), `$1${repl}`);
}
return c1 === c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Bash Shape Area
shape_area() {
local _n=$1
if (( _n > 1 )); then
echo $(( $(shape_area $(( _n - 1 ))) + 4 * (_n - 1) ))
else
echo 1
fi
}
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.
C++ Shape Area
long long shapeArea(long long n)
{
return n > 1 ? shapeArea(n - 1) + 4 * (n - 1) : 1;
}
This returns the area of the growing n-interesting polygon using the direct formula instead of building the shape.