-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_testcases.js
64 lines (52 loc) · 1.94 KB
/
process_testcases.js
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
const path = require('path');
const cheerio = require("cheerio");
const fs = require('fs');
function process_testCases(content, dir_path, vscode){
const $ = cheerio.load(content);
let input_testCases = "";
let output_testCases = "";
$('pre').each((i, element) => {
const tc = $(element).text().trim();
const lines = tc.split("\n").map((line) => line.trim()).filter(Boolean);
const input_line = lines.find((line) => line.startsWith("Input:")).replace("Input: ", "").trim();
const outputLine = lines.find((line) => line.startsWith("Output:")).replace("Output: ", "").trim();
// console.log(outputLine);
output_testCases+=outputLine;
output_testCases+="\n\n";
function formatLine(line) {
return line
.replace(/\b\w+(?=\s*=\s*)/g, (match) => `"${match}"`)
.replace(/\s*=\s*/g, ":");
}
const input_string = `{${formatLine(input_line)}}`;
const test_input = JSON.parse(input_string);
let result = "";
for (let key in test_input) {
const value = test_input[key];
if (Array.isArray(value) && value.every(Array.isArray)) {
const numRows = value.length;
const numCols = value[0].length;
result += `${numRows} ${numCols}\n`;
value.forEach((row) => {
result += `${row.join(" ")}\n`;
});
} else if (Array.isArray(value)) {
const size = value.length;
result += `${size}\n`;
result += `${value.join(" ")}\n`;
} else {
result += `${value}\n`;
}
}
input_testCases+=result;
input_testCases+="\n";
});
try {
fs.writeFileSync(path.join(dir_path, 'cph_input.txt'), input_testCases, 'utf8');
fs.writeFileSync(path.join(dir_path, 'cph_output.txt'), output_testCases, 'utf-8');
} catch (err) {
vscode.window.showErrorMessage(`Error writing to input.txt: ${err.message}`);
}
// console.log(input_testCases);
}
module.exports = process_testCases;