Small package to mock Codeforces Javascript IO functions.
npm i @ip-algorithmics/codeforces-io
Codeforces for Javascript/Typescript uses readline()
and print()
functions for input and output to the standard input/console.
This library exposes the functions in a manner that allows you to just copy the source code for the submission.
The functions can be imported both as ES6 modules or using the require
function. The difference lays in how you access it.
// using ES6 modules import - you need to have the "module" property set to "commonjs" in the package.json
import { readline, print } from '@ip-algorithmics/codeforces-io';
// using require
const codeForcesIO = require('@ip-algorithmics/codeforces-io');
const readline = codeForcesIO.readline;
const print = codeForcesIO.print;
// alternative
const { readline, print, console } = require('@ip-algorithmics/codeforces-io');
This function returns the next line read from the input.txt
file from the same folder.
You can iterate through the file calling it again.
let n = readline();
for (let i = 0; i < n; i++) {
let x = readline();
// do something with x
}
If you want to change the file path you must wrap the function inside your own.
import { readline as readlineCustom } from '@ip-algorithmics/codeforces-io';
function readline() {
return readlineCustom(filePath); // where filePath is the desired path to your file
}
// rest of the code
// alternatively using require
const codeForcesIO = require('@ip-algorithmics/codeforces-io');
const readlineCustom = codeForcesIO.readline;
const print = codeForcesIO.print;
function readline() {
return readlineCustom(filePath); // where filePath is the desired path to your file
}
readline()
can iterate through the entire file without needing the user to know the number of actual lines.
Codeforces provides it for lower level languages like C.
If you chose to iterate it like this, the end of file will return the empty string('').
let line;
while ((line = readline() && line != '')) {
// do something with line
}
// or
let line = readline();
while (line != '') {
// do something with line
line = readline();
}
This function just prints to the console the parameter. It is a glorified console.log
that saves whatever is printed in memory. You can then use testOutput()
to print to the screen if the test passed or failed.
let output = 'this is the output';
print(output);
This is an optional function reads the output.txt
from the same folder and iterates through the print
statements to check if they match the outputs.
You can set a different path passing it as a parameter.
If you use Typescript you will notice that the print
function is already declared in the DOM
library. To circumvent this issue I exported console.log
as print
and the entire console
object as console
for the cases where is needed for tracing or debugging.
Statement: given the input, print it
// input.txt
This is the input
import { readline, print } from '@ip-algorithmics/codeforces-io';
let firstLine = readline();
print(firstLine); //prints: This is the input
Statement: given the input - first line is the number of cases, next lines are the number describing geo points separated by comma. Find and print the geo point that is not valid.
input.txt
3
-47,23
34,56
91,82
output.txt
-47,23
const { readline, print, testOutput } = require('@ip-algorithmics/codeforces-io');
let numberOfLines = parseInt(readline(), 10);
for (let i = 0; i < numberOfLines; i++) {
let x = readline()
.trim()
.split(',')
.map((y) => parseInt(y, 10));
if ((x[0] < 90 && x[0] > -90) || (x[1] < 90 && x[1] > -90)) {
print(x[0] + ',' + x[1]);
break;
}
}
testOutput(); // Result Passed
You can use this feature in 2 ways:
- Without installing globally the library using
npx @ip-algorithmics/codeforces-io
. - Installing globally the library
npm i -g @ip-algorithmics/codeforces-io
and then usingcf
in the command line.
path
- the path to the solution. E.g../ProblemA
. Defaults to./New Solution
.--f
or--file
- the name of the js/ts file to create. E.g.mainFile
. Defaults toindex
.--js
- Uses.js
extension instead of.ts
--cjs
- Usesrequire
instead of ES6import
/export
--c
or--comment
- Adds a comment at the beginning of the file. E.g. the link to the problem.
npx @ip-algorithmics/codeforces-io ./ProblemA --js --cjs
cf ./ProblemA --js --cjs
cf ./ProblemA
cf ./ProblemA --c http://link.to.problem
Codforces uses ES5 internally in both node and V8 so it has problems with const
, let
and features like spreading an array.
Personally I found that the best solution is to:
- add in the
package.json
the following script"build": "tsc --module es6 --moduleResolution node"
using it likenpm run build <path>
- manually run
tsc --module es6 --moduleResolution node <path>
- Problem folder generator - generates folder, input.txt, output.txt, index.ts or index.js, in input puts link to problem, and template imports, and test - Completed
CodeForces Solutions Codeforces - How to use Typescript/Javascript like a pro
Added solution generator capabilities.
Added testOutput()
function.