rm some any

This commit is contained in:
Olivier Gagnon 2022-07-14 23:03:54 -04:00
parent 8fe824e8cd
commit 86c0913bd3
6 changed files with 135 additions and 86 deletions

@ -74,8 +74,8 @@ export const GameOptionsSidebar = (props: IProps): React.ReactElement => {
const data = await saveObject.getImportDataFromString(base64Save); const data = await saveObject.getImportDataFromString(base64Save);
setImportData(data); setImportData(data);
setImportSaveOpen(true); setImportSaveOpen(true);
} catch (ex: any) { } catch (e: unknown) {
SnackbarEvents.emit(ex.toString(), ToastVariant.ERROR, 5000); SnackbarEvents.emit(String(e), ToastVariant.ERROR, 5000);
} }
} }
@ -84,8 +84,8 @@ export const GameOptionsSidebar = (props: IProps): React.ReactElement => {
try { try {
await saveObject.importGame(importData.base64); await saveObject.importGame(importData.base64);
} catch (ex: any) { } catch (e: unknown) {
SnackbarEvents.emit(ex.toString(), ToastVariant.ERROR, 5000); SnackbarEvents.emit(String(e), ToastVariant.ERROR, 5000);
} }
setImportSaveOpen(false); setImportSaveOpen(false);

@ -1,5 +1,5 @@
import { toNative } from "./toNative"; import { toNative } from "./toNative";
import * as libarg from "arg"; import libarg from "arg";
export function Flags(vargs: string[]): any { export function Flags(vargs: string[]): any {
return () => return () =>

@ -20,9 +20,11 @@ export function toNative(pseudoObj: any): any {
if (pseudoObj.hasOwnProperty("class") && pseudoObj.class === "Array") { if (pseudoObj.hasOwnProperty("class") && pseudoObj.class === "Array") {
nativeObj = []; nativeObj = [];
const length = defaultInterpreter.getProperty(pseudoObj, "length"); const length = defaultInterpreter.getProperty(pseudoObj, "length");
for (let i = 0; i < length; i++) { if (typeof length === "number") {
if (defaultInterpreter.hasProperty(pseudoObj, i)) { for (let i = 0; i < length; i++) {
nativeObj[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i)); if (defaultInterpreter.hasProperty(pseudoObj, i)) {
nativeObj[i] = toNative(defaultInterpreter.getProperty(pseudoObj, i));
}
} }
} }
} else { } else {

@ -94,7 +94,7 @@ export function ThemeEditorModal(props: IProps): React.ReactElement {
} }
function onColorChange(name: string, value: string): void { function onColorChange(name: string, value: string): void {
setCustomTheme((old: any) => { setCustomTheme((old: { [key: string]: string | undefined }) => {
old[name] = value; old[name] = value;
return old; return old;
}); });

@ -1 +1,9 @@
export declare let Interpreter: any; export declare class Interpreter {
constructor(code: string, opt_initFunc: (int: Interpreter, scope: Object) => void, lineOffset?: number);
getProperty(obj: Value, name: Value): Value;
hasProperty(obj: Value, name: Value): boolean;
}
// Object and Value are 2 different things in the interpreter;
export declare type Object = unknown;
export declare type Value = unknown;

@ -6,14 +6,14 @@ import { HammingEncode, HammingDecode } from "../utils/HammingCodeTools";
/* tslint:disable:completed-docs no-magic-numbers arrow-return-shorthand */ /* tslint:disable:completed-docs no-magic-numbers arrow-return-shorthand */
/* Function that generates a valid 'data' for a contract type */ /* Function that generates a valid 'data' for a contract type */
export type GeneratorFunc = () => any; export type GeneratorFunc = () => unknown;
/* Function that checks if the provided solution is the correct one */ /* Function that checks if the provided solution is the correct one */
export type SolverFunc = (data: any, answer: string) => boolean; export type SolverFunc = (data: unknown, answer: string) => boolean;
/* Function that returns a string with the problem's description. /* Function that returns a string with the problem's description.
Requires the 'data' of a Contract as input */ Requires the 'data' of a Contract as input */
export type DescriptionFunc = (data: any) => string; export type DescriptionFunc = (data: unknown) => string;
interface ICodingContractTypeMetadata { interface ICodingContractTypeMetadata {
desc: DescriptionFunc; desc: DescriptionFunc;
@ -49,10 +49,10 @@ function removeQuotesFromString(str: string): string {
return strCpy; return strCpy;
} }
function convert2DArrayToString(arr: any[][]): string { function convert2DArrayToString(arr: unknown[][]): string {
const components: string[] = []; const components: string[] = [];
arr.forEach((e: any) => { arr.forEach((e: unknown) => {
let s: string = e.toString(); let s = String(e);
s = ["[", s, "]"].join(""); s = ["[", s, "]"].join("");
components.push(s); components.push(s);
}); });
@ -62,7 +62,7 @@ function convert2DArrayToString(arr: any[][]): string {
export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
{ {
desc: (n: number): string => { desc: (n: unknown): string => {
return ["A prime factor is a factor that is a prime number.", `What is the largest prime factor of ${n}?`].join( return ["A prime factor is a factor that is a prime number.", `What is the largest prime factor of ${n}?`].join(
" ", " ",
); );
@ -73,7 +73,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Find Largest Prime Factor", name: "Find Largest Prime Factor",
numTries: 10, numTries: 10,
solver: (data: number, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "number") throw new Error("solver expected number");
let fac = 2; let fac = 2;
let n: number = data; let n: number = data;
while (n > (fac - 1) * (fac - 1)) { while (n > (fac - 1) * (fac - 1)) {
@ -87,7 +88,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (n: number[]): string => { desc: (_n: unknown): string => {
const n = _n as number[];
return [ return [
"Given the following integer array, find the contiguous subarray", "Given the following integer array, find the contiguous subarray",
"(containing at least one number) which has the largest sum and return that sum.", "(containing at least one number) which has the largest sum and return that sum.",
@ -108,7 +110,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Subarray with Maximum Sum", name: "Subarray with Maximum Sum",
numTries: 10, numTries: 10,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
const nums: number[] = data.slice(); const nums: number[] = data.slice();
for (let i = 1; i < nums.length; i++) { for (let i = 1; i < nums.length; i++) {
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]); nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
@ -118,7 +121,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (n: number): string => { desc: (n: unknown): string => {
if (typeof n !== "number") throw new Error("solver expected number");
return [ return [
"It is possible write four as a sum in exactly four different ways:\n\n", "It is possible write four as a sum in exactly four different ways:\n\n",
"&nbsp;&nbsp;&nbsp;&nbsp;3 + 1\n", "&nbsp;&nbsp;&nbsp;&nbsp;3 + 1\n",
@ -135,7 +139,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Total Ways to Sum", name: "Total Ways to Sum",
numTries: 10, numTries: 10,
solver: (data: number, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "number") throw new Error("solver expected number");
const ways: number[] = [1]; const ways: number[] = [1];
ways.length = data + 1; ways.length = data + 1;
ways.fill(0, 1); ways.fill(0, 1);
@ -149,7 +154,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: [number, number[]]): string => { desc: (_data: unknown): string => {
const data = _data as [number, number[]];
const n: number = data[0]; const n: number = data[0];
const s: number[] = data[1]; const s: number[] = data[1];
return [ return [
@ -178,7 +184,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Total Ways to Sum II", name: "Total Ways to Sum II",
numTries: 10, numTries: 10,
solver: (data: [number, number[]], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as [number, number[]];
// https://www.geeksforgeeks.org/coin-change-dp-7/?ref=lbp // https://www.geeksforgeeks.org/coin-change-dp-7/?ref=lbp
const n = data[0]; const n = data[0];
const s = data[1]; const s = data[1];
@ -194,7 +201,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (n: number[][]): string => { desc: (_n: unknown): string => {
const n = _n as number[][];
let d: string = [ let d: string = [
"Given the following array of arrays of numbers representing a 2D matrix,", "Given the following array of arrays of numbers representing a 2D matrix,",
"return the elements of the matrix as an array in spiral order:\n\n", "return the elements of the matrix as an array in spiral order:\n\n",
@ -252,7 +260,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Spiralize Matrix", name: "Spiralize Matrix",
numTries: 10, numTries: 10,
solver: (data: number[][], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[][];
const spiral: number[] = []; const spiral: number[] = [];
const m: number = data.length; const m: number = data.length;
const n: number = data[0].length; const n: number = data[0].length;
@ -299,11 +308,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
} }
} }
const sanitizedPlayerAns: string = removeBracketsFromArrayString(ans).replace(/\s/g, ""); const sanitizedPlayerAns = removeBracketsFromArrayString(ans).replace(/\s/g, "");
const playerAns: any[] = sanitizedPlayerAns.split(","); const playerAns = sanitizedPlayerAns.split(",").map((s) => parseInt(s));
for (let i = 0; i < playerAns.length; ++i) {
playerAns[i] = parseInt(playerAns[i], 10);
}
if (spiral.length !== playerAns.length) { if (spiral.length !== playerAns.length) {
return false; return false;
} }
@ -317,7 +323,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (arr: number[]): string => { desc: (_arr: unknown): string => {
const arr = _arr as number[];
return [ return [
"You are given the following array of integers:\n\n", "You are given the following array of integers:\n\n",
`${arr}\n\n`, `${arr}\n\n`,
@ -348,7 +355,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Array Jumping Game", name: "Array Jumping Game",
numTries: 1, numTries: 1,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
const n: number = data.length; const n: number = data.length;
let i = 0; let i = 0;
for (let reach = 0; i < n && i <= reach; ++i) { for (let reach = 0; i < n && i <= reach; ++i) {
@ -359,7 +367,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (arr: number[]): string => { desc: (_arr: unknown): string => {
const arr = _arr as number[];
return [ return [
"You are given the following array of integers:\n\n", "You are given the following array of integers:\n\n",
`${arr}\n\n`, `${arr}\n\n`,
@ -391,7 +400,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Array Jumping Game II", name: "Array Jumping Game II",
numTries: 3, numTries: 3,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
const n: number = data.length; const n: number = data.length;
let reach = 0; let reach = 0;
let jumps = 0; let jumps = 0;
@ -415,7 +425,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (arr: number[][]): string => { desc: (_arr: unknown): string => {
const arr = _arr as number[][];
return [ return [
"Given the following array of arrays of numbers representing a list of", "Given the following array of arrays of numbers representing a list of",
"intervals, merge all overlapping intervals.\n\n", "intervals, merge all overlapping intervals.\n\n",
@ -442,7 +453,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Merge Overlapping Intervals", name: "Merge Overlapping Intervals",
numTries: 15, numTries: 15,
solver: (data: number[][], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[][];
const intervals: number[][] = data.slice(); const intervals: number[][] = data.slice();
intervals.sort((a: number[], b: number[]) => { intervals.sort((a: number[], b: number[]) => {
return a[0] - b[0]; return a[0] - b[0];
@ -469,7 +481,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: string): string => { desc: (data: unknown): string => {
return [ return [
"Given the following string containing only digits, return", "Given the following string containing only digits, return",
"an array with all possible valid IP address combinations", "an array with all possible valid IP address combinations",
@ -495,17 +507,18 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Generate IP Addresses", name: "Generate IP Addresses",
numTries: 10, numTries: 10,
solver: (data: string, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "string") throw new Error("solver expected string");
const ret: string[] = []; const ret: string[] = [];
for (let a = 1; a <= 3; ++a) { for (let a = 1; a <= 3; ++a) {
for (let b = 1; b <= 3; ++b) { for (let b = 1; b <= 3; ++b) {
for (let c = 1; c <= 3; ++c) { for (let c = 1; c <= 3; ++c) {
for (let d = 1; d <= 3; ++d) { for (let d = 1; d <= 3; ++d) {
if (a + b + c + d === data.length) { if (a + b + c + d === data.length) {
const A: number = parseInt(data.substring(0, a), 10); const A = parseInt(data.substring(0, a), 10);
const B: number = parseInt(data.substring(a, a + b), 10); const B = parseInt(data.substring(a, a + b), 10);
const C: number = parseInt(data.substring(a + b, a + b + c), 10); const C = parseInt(data.substring(a + b, a + b + c), 10);
const D: number = parseInt(data.substring(a + b + c, a + b + c + d), 10); const D = parseInt(data.substring(a + b + c, a + b + c + d), 10);
if (A <= 255 && B <= 255 && C <= 255 && D <= 255) { if (A <= 255 && B <= 255 && C <= 255 && D <= 255) {
const ip: string = [A.toString(), ".", B.toString(), ".", C.toString(), ".", D.toString()].join(""); const ip: string = [A.toString(), ".", B.toString(), ".", C.toString(), ".", D.toString()].join("");
if (ip.length === data.length + 3) { if (ip.length === data.length + 3) {
@ -533,7 +546,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[]): string => { desc: (_data: unknown): string => {
const data = _data as number[];
return [ return [
"You are given the following array of stock prices (which are numbers)", "You are given the following array of stock prices (which are numbers)",
"where the i-th element represents the stock price on day i:\n\n", "where the i-th element represents the stock price on day i:\n\n",
@ -557,7 +571,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Algorithmic Stock Trader I", name: "Algorithmic Stock Trader I",
numTries: 5, numTries: 5,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
let maxCur = 0; let maxCur = 0;
let maxSoFar = 0; let maxSoFar = 0;
for (let i = 1; i < data.length; ++i) { for (let i = 1; i < data.length; ++i) {
@ -569,7 +584,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[]): string => { desc: (_data: unknown): string => {
const data = _data as number[];
return [ return [
"You are given the following array of stock prices (which are numbers)", "You are given the following array of stock prices (which are numbers)",
"where the i-th element represents the stock price on day i:\n\n", "where the i-th element represents the stock price on day i:\n\n",
@ -595,7 +611,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Algorithmic Stock Trader II", name: "Algorithmic Stock Trader II",
numTries: 10, numTries: 10,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
let profit = 0; let profit = 0;
for (let p = 1; p < data.length; ++p) { for (let p = 1; p < data.length; ++p) {
profit += Math.max(data[p] - data[p - 1], 0); profit += Math.max(data[p] - data[p - 1], 0);
@ -605,7 +622,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[]): string => { desc: (_data: unknown): string => {
const data = _data as number[];
return [ return [
"You are given the following array of stock prices (which are numbers)", "You are given the following array of stock prices (which are numbers)",
"where the i-th element represents the stock price on day i:\n\n", "where the i-th element represents the stock price on day i:\n\n",
@ -631,9 +649,10 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Algorithmic Stock Trader III", name: "Algorithmic Stock Trader III",
numTries: 10, numTries: 10,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
let hold1: number = Number.MIN_SAFE_INTEGER; const data = _data as number[];
let hold2: number = Number.MIN_SAFE_INTEGER; let hold1 = Number.MIN_SAFE_INTEGER;
let hold2 = Number.MIN_SAFE_INTEGER;
let release1 = 0; let release1 = 0;
let release2 = 0; let release2 = 0;
for (const price of data) { for (const price of data) {
@ -647,9 +666,10 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: any[]): string => { desc: (_data: unknown): string => {
const k: number = data[0]; const data = _data as [number, number[]];
const prices: number[] = data[1]; const k = data[0];
const prices = data[1];
return [ return [
"You are given the following array with two elements:\n\n", "You are given the following array with two elements:\n\n",
`[${k}, [${prices}]]\n\n`, `[${k}, [${prices}]]\n\n`,
@ -665,9 +685,9 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
].join(" "); ].join(" ");
}, },
difficulty: 8, difficulty: 8,
gen: (): any[] => { gen: (): [number, number[]] => {
const k: number = getRandomInt(2, 10); const k = getRandomInt(2, 10);
const len: number = getRandomInt(3, 50); const len = getRandomInt(3, 50);
const prices: number[] = []; const prices: number[] = [];
prices.length = len; prices.length = len;
for (let i = 0; i < len; ++i) { for (let i = 0; i < len; ++i) {
@ -678,7 +698,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Algorithmic Stock Trader IV", name: "Algorithmic Stock Trader IV",
numTries: 10, numTries: 10,
solver: (data: any[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as [number, number[]];
const k: number = data[0]; const k: number = data[0];
const prices: number[] = data[1]; const prices: number[] = data[1];
@ -717,7 +738,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[][]): string => { desc: (_data: unknown): string => {
const data = _data as number[][];
function createTriangleRecurse(data: number[][], level = 0): string { function createTriangleRecurse(data: number[][], level = 0): string {
const numLevels: number = data.length; const numLevels: number = data.length;
if (level >= numLevels) { if (level >= numLevels) {
@ -771,7 +793,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Minimum Path Sum in a Triangle", name: "Minimum Path Sum in a Triangle",
numTries: 10, numTries: 10,
solver: (data: number[][], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[][];
const n: number = data.length; const n: number = data.length;
const dp: number[] = data[n - 1].slice(); const dp: number[] = data[n - 1].slice();
for (let i = n - 2; i > -1; --i) { for (let i = n - 2; i > -1; --i) {
@ -784,7 +807,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[]): string => { desc: (_data: unknown): string => {
const data = _data as number[];
const numRows = data[0]; const numRows = data[0];
const numColumns = data[1]; const numColumns = data[1];
return [ return [
@ -808,7 +832,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Unique Paths in a Grid I", name: "Unique Paths in a Grid I",
numTries: 10, numTries: 10,
solver: (data: number[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[];
const n: number = data[0]; // Number of rows const n: number = data[0]; // Number of rows
const m: number = data[1]; // Number of columns const m: number = data[1]; // Number of columns
const currentRow: number[] = []; const currentRow: number[] = [];
@ -827,7 +852,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: number[][]): string => { desc: (_data: unknown): string => {
const data = _data as number[][];
let gridString = ""; let gridString = "";
for (const line of data) { for (const line of data) {
gridString += `${line.toString()},\n`; gridString += `${line.toString()},\n`;
@ -876,7 +902,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Unique Paths in a Grid II", name: "Unique Paths in a Grid II",
numTries: 10, numTries: 10,
solver: (data: number[][], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[][];
const obstacleGrid: number[][] = []; const obstacleGrid: number[][] = [];
obstacleGrid.length = data.length; obstacleGrid.length = data.length;
for (let i = 0; i < obstacleGrid.length; ++i) { for (let i = 0; i < obstacleGrid.length; ++i) {
@ -900,7 +927,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
{ {
name: "Shortest Path in a Grid", name: "Shortest Path in a Grid",
desc: (data: number[][]): string => { desc: (_data: unknown): string => {
const data = _data as number[][];
return [ return [
"You are located in the top-left corner of the following grid:\n\n", "You are located in the top-left corner of the following grid:\n\n",
`&nbsp;&nbsp;[${data.map((line) => "[" + line + "]").join(",\n&nbsp;&nbsp;&nbsp;")}]\n\n`, `&nbsp;&nbsp;[${data.map((line) => "[" + line + "]").join(",\n&nbsp;&nbsp;&nbsp;")}]\n\n`,
@ -950,7 +978,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return grid; return grid;
}, },
solver: (data: number[][], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as number[][];
const width = data[0].length; const width = data[0].length;
const height = data.length; const height = data.length;
const dstY = height - 1; const dstY = height - 1;
@ -1033,7 +1062,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: string): string => { desc: (data: unknown): string => {
return [ return [
"Given the following string:\n\n", "Given the following string:\n\n",
`${data}\n\n`, `${data}\n\n`,
@ -1073,7 +1102,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Sanitize Parentheses in Expression", name: "Sanitize Parentheses in Expression",
numTries: 10, numTries: 10,
solver: (data: string, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "string") throw new Error("solver expected string");
let left = 0; let left = 0;
let right = 0; let right = 0;
const res: string[] = []; const res: string[] = [];
@ -1141,7 +1171,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
}, },
{ {
desc: (data: any[]): string => { desc: (_data: unknown): string => {
const data = _data as [string, number];
const digits: string = data[0]; const digits: string = data[0];
const target: number = data[1]; const target: number = data[1];
@ -1166,7 +1197,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
].join(" "); ].join(" ");
}, },
difficulty: 10, difficulty: 10,
gen: (): any[] => { gen: (): [string, number] => {
const numDigits = getRandomInt(4, 12); const numDigits = getRandomInt(4, 12);
const digitsArray: string[] = []; const digitsArray: string[] = [];
digitsArray.length = numDigits; digitsArray.length = numDigits;
@ -1185,9 +1216,10 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
}, },
name: "Find All Valid Math Expressions", name: "Find All Valid Math Expressions",
numTries: 10, numTries: 10,
solver: (data: any[], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const num: string = data[0]; const data = _data as [string, number];
const target: number = data[1]; const num = data[0];
const target = data[1];
function helper( function helper(
res: string[], res: string[],
@ -1253,7 +1285,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "HammingCodes: Integer to Encoded Binary", name: "HammingCodes: Integer to Encoded Binary",
numTries: 10, numTries: 10,
difficulty: 5, difficulty: 5,
desc: (n: number): string => { desc: (n: unknown): string => {
return [ return [
"You are given the following decimal Value: \n", "You are given the following decimal Value: \n",
`${n} \n`, `${n} \n`,
@ -1272,7 +1304,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
gen: (): number => { gen: (): number => {
return getRandomInt(Math.pow(2, 4), Math.pow(2, getRandomInt(1, 57))); return getRandomInt(Math.pow(2, 4), Math.pow(2, getRandomInt(1, 57)));
}, },
solver: (data: number, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "number") throw new Error("solver expected number");
return ans === HammingEncode(data); return ans === HammingEncode(data);
}, },
}, },
@ -1280,7 +1313,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "HammingCodes: Encoded Binary to Integer", name: "HammingCodes: Encoded Binary to Integer",
difficulty: 8, difficulty: 8,
numTries: 10, numTries: 10,
desc: (n: string): string => { desc: (n: unknown): string => {
return [ return [
"You are given the following encoded binary string: \n", "You are given the following encoded binary string: \n",
`'${n}' \n`, `'${n}' \n`,
@ -1304,7 +1337,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
} }
return _buildArray.join(""); return _buildArray.join("");
}, },
solver: (data: string, ans: string): boolean => { solver: (data: unknown, ans: string): boolean => {
if (typeof data !== "string") throw new Error("solver expected string");
return parseInt(ans, 10) === HammingDecode(data); return parseInt(ans, 10) === HammingDecode(data);
}, },
}, },
@ -1312,7 +1346,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "Proper 2-Coloring of a Graph", name: "Proper 2-Coloring of a Graph",
difficulty: 7, difficulty: 7,
numTries: 5, numTries: 5,
desc: (data: [number, [number, number][]]): string => { desc: (_data: unknown): string => {
const data = _data as [number, [number, number][]];
return [ return [
`You are given the following data, representing a graph:\n`, `You are given the following data, representing a graph:\n`,
`${JSON.stringify(data)}\n`, `${JSON.stringify(data)}\n`,
@ -1359,7 +1394,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
} }
//Randomize array in-place using Durstenfeld shuffle algorithm. //Randomize array in-place using Durstenfeld shuffle algorithm.
function shuffle(array: any[]): void { function shuffle<T>(array: T[]): void {
for (let i = array.length - 1; i > 0; i--) { for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; [array[i], array[j]] = [array[j], array[i]];
@ -1382,7 +1417,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return [n + m, edges]; return [n + m, edges];
}, },
solver: (data: [number, [number, number][]], ans: string): boolean => { solver: (_data: unknown, ans: string): boolean => {
const data = _data as [number, [number, number][]];
//Case where the player believes there is no solution. //Case where the player believes there is no solution.
//Attempt to construct one to check if this is correct. //Attempt to construct one to check if this is correct.
if (ans == "[]") { if (ans == "[]") {
@ -1463,7 +1499,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "Compression I: RLE Compression", name: "Compression I: RLE Compression",
difficulty: 2, difficulty: 2,
numTries: 10, numTries: 10,
desc: (plaintext: string): string => { desc: (plaintext: unknown): string => {
return [ return [
"Run-length encoding (RLE) is a data compression technique which encodes data as a series of runs of", "Run-length encoding (RLE) is a data compression technique which encodes data as a series of runs of",
"a repeated single character. Runs are encoded as a length, followed by the character itself. Lengths", "a repeated single character. Runs are encoded as a length, followed by the character itself. Lengths",
@ -1503,7 +1539,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
return plain.substring(0, length); return plain.substring(0, length);
}, },
solver: (plain: string, ans: string): boolean => { solver: (plain: unknown, ans: string): boolean => {
if (typeof plain !== "string") throw new Error("solver expected string");
if (ans.length % 2 !== 0) { if (ans.length % 2 !== 0) {
return false; return false;
} }
@ -1542,7 +1579,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "Compression II: LZ Decompression", name: "Compression II: LZ Decompression",
difficulty: 4, difficulty: 4,
numTries: 10, numTries: 10,
desc: (compressed: string): string => { desc: (compressed: unknown): string => {
return [ return [
"Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to", "Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to",
"earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk", "earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk",
@ -1569,7 +1606,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
gen: (): string => { gen: (): string => {
return comprLZEncode(comprLZGenerate()); return comprLZEncode(comprLZGenerate());
}, },
solver: (compr: string, ans: string): boolean => { solver: (compr: unknown, ans: string): boolean => {
if (typeof compr !== "string") throw new Error("solver expected string");
return ans === comprLZDecode(compr); return ans === comprLZDecode(compr);
}, },
}, },
@ -1577,7 +1615,7 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
name: "Compression III: LZ Compression", name: "Compression III: LZ Compression",
difficulty: 10, difficulty: 10,
numTries: 10, numTries: 10,
desc: (plaintext: string): string => { desc: (plaintext: unknown): string => {
return [ return [
"Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to", "Lempel-Ziv (LZ) compression is a data compression technique which encodes data using references to",
"earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk", "earlier parts of the data. In this variant of LZ, data is encoded in two types of chunk. Each chunk",
@ -1607,7 +1645,8 @@ export const codingContractTypesMetadata: ICodingContractTypeMetadata[] = [
gen: (): string => { gen: (): string => {
return comprLZGenerate(); return comprLZGenerate();
}, },
solver: (plain: string, ans: string): boolean => { solver: (plain: unknown, ans: string): boolean => {
if (typeof plain !== "string") throw new Error("solver expected string");
return comprLZDecode(ans) === plain && ans.length <= comprLZEncode(plain).length; return comprLZDecode(ans) === plain && ans.length <= comprLZEncode(plain).length;
}, },
}, },