Skip to content

Instantly share code, notes, and snippets.

@damdafayton
Last active August 9, 2022 18:49
Show Gist options
  • Save damdafayton/f7808221788dd37f13540e9ebd6fdc5f to your computer and use it in GitHub Desktop.
Save damdafayton/f7808221788dd37f13540e9ebd6fdc5f to your computer and use it in GitHub Desktop.
Testovac
const fs = require("fs");
const FILES = ["example", "example_big", "test"];
const FILE_NAME = FILES[2];
const source = require(`./${FILE_NAME}.in.json`);
let allResults = [];
source.testCases.forEach((c, i) => {
let { dictionary, queries } = c;
let map = {};
// map the dictionary
dictionary.forEach((pair) => {
let p1 = pair[0].toLowerCase();
let p2 = pair[1].toLowerCase();
if (p1 === p2) return;
map[p1] = map[p1] || [];
map[p2] = map[p2] || [];
map[p1] = (!map[p1].includes(p2) && [...map[p1], p2]) || map[p1];
map[p2] = (!map[p2].includes(p1) && [...map[p2], p1]) || map[p2];
});
let lowerQueries = queries.map((pair) => {
if (pair[0].toLowerCase() === pair[1].toLowerCase()) {
return "synonymous";
} else {
return [pair[0].toLowerCase(), pair[1].toLowerCase()];
}
});
result = [];
lowerQueries.forEach((pair, idx) => {
let flag = true;
let different = true;
if (typeof pair === "string") {
different = false;
flag = false;
} else {
pair.forEach((el, pidx) => {
let counter = 0;
flag = true;
let otherEl = pair.filter((it) => it !== el);
let mapCopy = { ...map };
otherEl = otherEl[0];
mapCopy[el] = mapCopy[el] || [];
let keys = [...mapCopy[el]];
let checkList = [...mapCopy[el]];
let trackMap = {};
while (flag) {
if (checkList.includes(otherEl)) {
different = false;
flag = false;
}
if (keys.length) {
let newKey = keys.shift();
checkList = [...mapCopy[newKey]];
if (!trackMap[newKey]) {
trackMap[newKey] = true;
keys = [...keys, ...checkList];
}
} else {
flag = false;
}
}
});
}
result.push(different ? "different" : "synonyms");
});
allResults = [...allResults, ...result];
});
// console.log(allResults.join("\n"));
fs.writeFileSync(
`./damdafayton-${FILE_NAME}-result.txt`,
allResults.join("\n")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment