-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCP_template.js
50 lines (41 loc) · 1.01 KB
/
CP_template.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
'use strict'
const fs = require("fs");
function* generateTokenizer(s) {
//generator function which yields the next token from the input stream stored in s
let token = '';
let ind = 0, len = s.length;
while (ind < len) {
while (ind < len && (s[ind] == ' ' || s[ind] == '\n')) {
++ind;
}
while (ind < len && (s[ind] != ' ' && s[ind] != '\n')) {
token += s[ind++];
}
yield token;
token = '';
}
}
function read() {
return new Promise((resolve) => {
fs.readFile('input.txt', 'utf-8', (err, data) => {
if (err) {
throw new Error('File reading error!');
}
resolve(data);
});
});
}
async function main() {
let data = await read();
let tokenizer = generateTokenizer(data)
// code starts from here
// Below code is an example code snippet for reading an input integer array
let n = tokenizer.next().value;
let arr = [];
for (let i = 0; i < n; ++i) {
arr.push(+tokenizer.next().value);
}
console.log(n);
console.log(arr);
}
main();