JavaScript solution runner

Revision en3, by romanrich89, 2023-04-07 18:13:44

Hi competitive programmers and especially who writes problem solutions in JavaScript. I'm amoung them, yes I'm a little crazy too;)

I've already participated in two contests. In first for div 2 I've managed to solve 2 problems, but in next for div 3 I've just solved 1 and was a little disappointed about that. I decided to skip next one but curriousity took over me and I've looked to first problem in such an epic contest Educational Codeforces Round 146 (Rated for Div. 2). https://mirror.codeforces.com/contest/1814/problem/A after some thinking about and scratching some expressions on papare i saw a math model of solution and it looked like to transfer 2 bits over wire and i jumped in contest room but failed( My assumptions were wrong( But thanks to Codeforces I can see other's solutions I've updated my JS code but keep failing. Thanks to https://mirror.codeforces.com/profile/shakhnoov I figure it out and now I present you JavaScript solution runner CodeforcesIO and for testing CodeforcesTester. You if you found it helpful for you than I'm really glade! Thank you and happy coding! _

const { CodeforcesTester } = require('../lib/codeforces-tester');

// test input/output data
const tester = new CodeforcesTester()
    .inputData(`
2
1 2
3 5
`).outputData(`
3
8

`)

// execute script: node Template.js
// This template can run code in both envs: JavaScript V8 4.8.0, Node.js 12.16.3
// NOTE: Always use Node.js 12.16.3 because it supports ES6 features and BigInt which sometimes really important
// copy and send code with solution to https://mirror.codeforces.com/ from HERE below (for engine JavaScript V8 4.8.0 IMPORTANT to add at start of script: "use strict";)
"use strict";

class CodeforcesIO {
    /**
     * @param solve a callback function which provides functionality to work with input/output data
     * ```js
     * new CodeforcesIO((readline, print) => {
     *  const echo = readline()
     *  print(echo)
     * })
     * ```
     */
    constructor(solve) {
        this.lines = []
        this.lineNum = 0
        this.result = []

        if (typeof tester !== 'undefined') { // test input/output data are set
            solve(tester.readline.bind(tester), tester.print.bind(tester));
            return
        }

        if (typeof process !== 'undefined') { // for Node.js 12.16.3
            require('readline').createInterface({
                input: process.stdin
            }).on('line', (line) => {
                this.lines.push(line);
            }).on('close', () => {
                solve(this.readline.bind(this), this.print.bind(this));
                process.stdout.write(this.result.join('\n'));
            })
            return;
        }

        solve(readline, print); // readline, print are defined globally in JavaScript V8 4.8.0
    }

    readline() {
        return this.lines[this.lineNum++]
    }

    print(res) {
        this.result.push(res);
    }
}

// your solution starts here. Good luck!
const solution = (a, b) => {
    return a + b
}

new CodeforcesIO((readline, print) => {
    const t = readline() // first line of input usually gives the no. of test cases,i.e, the no. of lines ahead.

    for (let i = 0; i < t; i++) {
        const lineArgs = readline().split(' ')
        print(solution(+lineArgs[0], +lineArgs[1]))
    }
})

_

class CodeforcesTester {
    _inputGenerator
    _outputGenerator

    * _generateReadline(testCases) {
        const arr = testCases.split('\n').filter(s => s);
        const lastIndex = arr.length - 1

        for (let i = 0; i < lastIndex; i++) {
            yield arr[i]
        }

        return arr[lastIndex]
    }

    /**
     * @param {string} inputFileContent copy input data from problem
     * @return {CodeforcesTester}
     */
    inputData(inputFileContent) {
        this._inputGenerator = this._generateReadline(inputFileContent);
        return this
    }

    /**
     * @param {string} outputFileContent copy output data from problem
     * @return {CodeforcesTester}
     */
    outputData(outputFileContent) {
        this._outputGenerator = this._generateReadline(outputFileContent);
        return this
    }

    readline() {
        if (this._inputGenerator) return this._inputGenerator.next().value
        throw new Error('Input data was not set')
    }

    print(str) {
        if (this._outputGenerator) {
            const nextValue = this._outputGenerator.next().value
            if (str != nextValue) throw new Error(str + ' <-- Incorrect output line, should be --> ' + nextValue)
        }
        console.log(str)
    }
}

module.exports = { CodeforcesTester };

_

Tags js, javascript, nodejs, um_nik

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en25 English romanrich89 2023-04-07 20:54:03 83
en24 English romanrich89 2023-04-07 20:50:04 1 Tiny change: 'er>\n\n### Oh, I ' -> 'er>\n\n#### Oh, I '
en23 English romanrich89 2023-04-07 20:00:36 0 (published)
en22 English romanrich89 2023-04-07 19:58:56 98
en21 English romanrich89 2023-04-07 19:58:03 92
en20 English romanrich89 2023-04-07 19:57:19 43
en19 English romanrich89 2023-04-07 19:56:09 94
en18 English romanrich89 2023-04-07 19:50:46 29
en17 English romanrich89 2023-04-07 19:47:32 6
en16 English romanrich89 2023-04-07 19:45:06 81
en15 English romanrich89 2023-04-07 19:42:56 50
en14 English romanrich89 2023-04-07 19:42:15 42
en13 English romanrich89 2023-04-07 19:41:17 1181
en12 English romanrich89 2023-04-07 19:31:24 59
en11 English romanrich89 2023-04-07 19:28:00 10
en10 English romanrich89 2023-04-07 19:27:03 26
en9 English romanrich89 2023-04-07 19:23:10 1180
en8 English romanrich89 2023-04-07 18:35:29 48
en7 English romanrich89 2023-04-07 18:30:48 83
en6 English romanrich89 2023-04-07 18:22:10 86
en5 English romanrich89 2023-04-07 18:19:31 67
en4 English romanrich89 2023-04-07 18:15:49 43
en3 English romanrich89 2023-04-07 18:13:44 330
en2 English romanrich89 2023-04-07 16:39:00 813
en1 English romanrich89 2023-04-07 16:13:34 3713 Initial revision (saved to drafts)