Spaces:
Running
Running
File size: 3,201 Bytes
1113aa2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | // ===========================================================================
// Cloudflare Worker — hidden "blackbox" backend.
//
// The real implementations live ONLY here, on Cloudflare's edge. They are
// never sent to the browser, so students cannot download or read them.
// The frontend only sends inputs and receives outputs (black-box RPC).
//
// Deploy via the Cloudflare dashboard: Workers & Pages -> Create -> Worker
// -> paste this file -> Deploy. Then copy the *.workers.dev URL.
// ===========================================================================
// ---- Hidden problem implementations (keep behaviour matching the originals) ----
const SOLUTIONS = {
Q1: {
grade(score) {
if (score >= 85) return "HD";
if (score >= 75) return "DN";
if (score >= 65) return "CR";
if (score >= 50) return "PS";
return "FL";
},
clamp(x, lo, hi) {
return Math.max(lo, Math.min(x, hi));
},
fizzbuzz(n) {
if (n % 15 === 0) return "FizzBuzz";
if (n % 3 === 0) return "Fizz";
if (n % 5 === 0) return "Buzz";
return String(n);
},
},
Q2: {
is_palindrome(s) {
const c = [...String(s).toLowerCase()].filter((ch) => /[a-z0-9]/.test(ch)).join("");
return c === [...c].reverse().join("");
},
count_vowels(s) {
return [...String(s).toLowerCase()].filter((ch) => "aeiou".includes(ch)).length;
},
reverse_words(s) {
const parts = String(s).trim().split(/\s+/).filter(Boolean);
return parts.reverse().join(" ");
},
},
};
const CORS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
function json(obj, status = 200) {
return new Response(JSON.stringify(obj), {
status,
headers: { "Content-Type": "application/json", ...CORS },
});
}
export default {
async fetch(request) {
if (request.method === "OPTIONS") return new Response(null, { headers: CORS });
const url = new URL(request.url);
const problem = url.searchParams.get("problem");
const func = url.searchParams.get("func");
// Health check / root
if (!problem) {
return json({ ok: true, problems: Object.keys(SOLUTIONS) });
}
const impl = SOLUTIONS[problem];
if (!impl) return json({ error: `unknown problem: ${problem}` }, 404);
// Metadata: list the callable function names for a problem (names are NOT secret)
if (!func) {
return json({ problem, functions: Object.keys(impl) });
}
// A black-box call: run the hidden function with the given args
const fn = impl[func];
if (typeof fn !== "function") {
return json({ error: `unknown function: ${problem}.${func}` }, 404);
}
let args;
try {
args = JSON.parse(url.searchParams.get("args") || "[]");
if (!Array.isArray(args)) throw new Error("args must be a JSON array");
} catch (e) {
return json({ error: "bad args: " + e.message }, 400);
}
try {
const result = fn(...args);
return json({ result });
} catch (e) {
return json({ error: String(e && e.message || e) }, 400);
}
},
};
|