Lisp Perm Missing Element
(defun perm-missing-element (a)
(let* ((sorted (sort (copy-list a) #'<))
(vec (coerce sorted 'vector)))
(loop for k from 0 below (length vec)
do (when (/= (aref vec k) (1+ k))
(return-from perm-missing-element (1+ k))))
(1+ (length vec))))
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
PHP Perm Missing Element
function permMissingElement($a): int
{
sort($a);
foreach ($a as $k => $v) {
if ($v !== $k + 1) {
return $k + 1;
}
}
return count($a) + 1;
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Python Perm Missing Element
def perm_missing_element(a: list[int]) -> int:
a = sorted(a)
for k, v in enumerate(a):
if v != k + 1:
return k + 1
return len(a) + 1
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Rust Perm Missing Element
fn perm_missing_element(mut a: Vec<i64>) -> i64 {
a.sort();
for (k, &v) in a.iter().enumerate() {
if v != k as i64 + 1 {
return k as i64 + 1;
}
}
a.len() as i64 + 1
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
TypeScript Perm Missing Element
function permMissingElement(a: number[]): number {
const sorted = [...a].sort((x, y) => x - y);
for (let k = 0; k < sorted.length; k++) {
if (sorted[k] !== k + 1) {
return k + 1;
}
}
return sorted.length + 1;
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Bash Plagiarism Check
plagiarism_check() {
local -n _code1="$1"
local -n _code2="$2"
local _c1 _c2
_c1=$(IFS=' '; echo "${_code1[*]}")
_c2=$(IFS=' '; echo "${_code2[*]}")
if [[ "$_c1" == "$_c2" ]]; then
echo false
return
fi
local -a _d1 _d2
IFS=' ' read -ra _d1 <<< "$_c1"
IFS=' ' read -ra _d2 <<< "$_c2"
local -A _rcand
local -a _rcand_keys=()
local _k
for _k in "${!_d1[@]}"; do
local _v=${_d1[$_k]}
local _w=${_d2[$_k]}
if [[ "$_v" != "$_w" ]] && ! [[ "$_v" =~ ^-?[0-9]+$ ]]; then
if [[ -z "${_rcand[$_v]:-}" ]]; then
_rcand_keys+=("$_v")
fi
_rcand[$_v]=$_w
fi
done
local _orig _repl
for _orig in "${_rcand_keys[@]}"; do
_repl=${_rcand[$_orig]}
_c1=$(echo "$_c1" | sed -E "s/(^|[^A-Za-z0-9_])${_orig}([^A-Za-z0-9_]|$)/\1PLACEHOLDER${_orig}\2/g")
_c1=$(echo "$_c1" | sed -E "s/(^|[^A-Za-z0-9_])${_orig}([A-Za-z0-9_]|$)/\1PLACEHOLDER${_orig}\2/g")
done
for _orig in "${_rcand_keys[@]}"; do
_repl=${_rcand[$_orig]}
_c1=$(echo "$_c1" | sed -E "s/(^|[^A-Za-z0-9_])PLACEHOLDER${_orig}([^A-Za-z0-9_]|$)/\1${_repl}\2/g")
_c1=$(echo "$_c1" | sed -E "s/(^|[^A-Za-z0-9_])PLACEHOLDER${_orig}([A-Za-z0-9_]|$)/\1${_repl}\2/g")
done
if [[ "$_c1" == "$_c2" ]]; then echo true; else echo false; fi
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
C++ Plagiarism Check
#include <algorithm>
#include <cctype>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
bool plagiarismCheck(const std::vector<std::string>& code1, const std::vector<std::string>& code2)
{
auto join = [](const std::vector<std::string>& lines) {
std::string result;
for (std::size_t i = 0; i < lines.size(); ++i) {
if (i > 0) {
result += ' ';
}
result += lines[i];
}
return result;
};
std::string c1 = join(code1);
std::string c2 = join(code2);
if (c1 == c2) {
return false;
}
std::vector<std::string> d1;
std::vector<std::string> d2;
std::regex wordRe(R"(\w+)");
for (auto it = std::sregex_iterator(c1.begin(), c1.end(), wordRe); it != std::sregex_iterator(); ++it) {
d1.push_back(it->str());
}
for (auto it = std::sregex_iterator(c2.begin(), c2.end(), wordRe); it != std::sregex_iterator(); ++it) {
d2.push_back(it->str());
}
std::unordered_map<std::string, std::string> rCand;
for (std::size_t k = 0; k < d1.size() && k < d2.size(); ++k) {
bool isNumeric = !d1[k].empty() && std::all_of(d1[k].begin(), d1[k].end(), [](unsigned char c) { return std::isdigit(c); });
if (d1[k] != d2[k] && !isNumeric) {
rCand[d1[k]] = d2[k];
}
}
for (const auto& [orig, repl] : rCand) {
c1 = std::regex_replace(c1, std::regex("(\\W)" + orig + "(\\W*)"), "$1PLACEHOLDER" + orig + "$2");
c1 = std::regex_replace(c1, std::regex("(\\W)" + orig), "$1PLACEHOLDER" + orig);
}
for (const auto& [orig, repl] : rCand) {
c1 = std::regex_replace(c1, std::regex("(\\W)PLACEHOLDER" + orig + "(\\W)"), "$1" + repl + "$2");
c1 = std::regex_replace(c1, std::regex("(\\W)PLACEHOLDER" + orig), "$1" + repl);
}
return c1 == c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
C# Plagiarism Check
static bool PlagiarismCheck(string[] code1, string[] code2)
{
var c1 = string.Join(" ", code1);
var c2 = string.Join(" ", code2);
if (c1 == c2)
{
return false;
}
var d1 = Regex.Matches(c1, @"\w+");
var d2 = Regex.Matches(c2, @"\w+");
var rCand = new Dictionary<string, string>();
for (int k = 0; k < d1.Count; k++)
{
var v = d1[k].Value;
var w = d2[k].Value;
if (v != w && !double.TryParse(v, out _))
{
rCand[v] = w;
}
}
foreach (var pair in rCand)
{
var orig = Regex.Escape(pair.Key);
c1 = Regex.Replace(c1, $@"(\W){orig}(\W*)", $"$1PLACEHOLDER{pair.Key}$2");
c1 = Regex.Replace(c1, $@"(\W){orig}", $"$1PLACEHOLDER{pair.Key}");
}
foreach (var pair in rCand)
{
var orig = Regex.Escape(pair.Key);
c1 = Regex.Replace(c1, $@"(\W)PLACEHOLDER{orig}(\W)", $"$1{pair.Value}$2");
c1 = Regex.Replace(c1, $@"(\W)PLACEHOLDER{orig}", $"$1{pair.Value}");
}
return c1 == c2;
}
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Elixir Plagiarism Check
defmodule PlagiarismCheck do
def plagiarism_check(code1, code2) do
c1 = Enum.join(code1, " ")
c2 = Enum.join(code2, " ")
if c1 == c2 do
false
else
words1 = ~r/\w+/ |> Regex.scan(c1) |> List.flatten()
words2 = ~r/\w+/ |> Regex.scan(c2) |> List.flatten()
candidates =
words1
|> Enum.zip(words2)
|> Enum.reduce(%{}, fn {w1, w2}, acc ->
if w1 != w2 and not numeric?(w1), do: Map.put(acc, w1, w2), else: acc
end)
c1_placeheld =
Enum.reduce(candidates, c1, fn {orig, _repl}, acc ->
replace_word(acc, orig, "PLACEHOLDER" <> orig)
end)
c1_final =
Enum.reduce(candidates, c1_placeheld, fn {orig, repl}, acc ->
replace_word(acc, "PLACEHOLDER" <> orig, repl)
end)
c1_final == c2
end
end
defp numeric?(w) do
case Integer.parse(w) do
{_n, ""} -> true
_ -> false
end
end
defp replace_word(text, target, replacement) do
escaped = Regex.escape(target)
text
|> String.replace(~r/(\W)#{escaped}(\W)/, "\\1#{replacement}\\2")
|> String.replace(~r/(\W)#{escaped}/, "\\1#{replacement}")
end
end
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.
Erlang Plagiarism Check
-module(plagiarism_check).
-export([plagiarism_check/2]).
%% Re-derived as a token-isomorphism check instead of porting PHP's
%% placeholder/regex substitution dance: tokenize both snippets, build a
%% one-way rename map from the mismatched, non-numeric tokens of Code1 onto
%% Code2, then verify applying that map to every token of Code1 reproduces
%% Code2 exactly.
plagiarism_check(Code1, Code2) ->
C1 = string:join(Code1, " "),
C2 = string:join(Code2, " "),
case C1 =:= C2 of
true ->
false;
false ->
D1 = tokenize(C1),
D2 = tokenize(C2),
Map = lists:foldl(fun({T1, T2}, Acc) ->
case T1 =/= T2 andalso not is_numeric_token(T1) of
true -> maps:put(T1, T2, Acc);
false -> Acc
end
end, #{}, lists:zip(D1, D2)),
[maps:get(T, Map, T) || T <- D1] =:= D2
end.
tokenize(S) ->
case re:run(S, "[A-Za-z0-9_]+", [global, {capture, all, list}]) of
{match, Matches} -> [Tok || [Tok] <- Matches];
nomatch -> []
end.
is_numeric_token(T) ->
case string:to_integer(T) of
{_, ""} -> true;
_ ->
case string:to_float(T) of
{_, ""} -> true;
_ -> false
end
end.
This flattens both snippets, tries consistent identifier replacements, and checks whether the rewritten code matches.