npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ip-algorithmics/codeforces-io

v1.1.2

Published

Codeforces IO functions

Downloads

26

Readme

CodeForces IO

Small package to mock Codeforces Javascript IO functions.

Build Test NPM

Install

npm i @ip-algorithmics/codeforces-io

Intro

Codeforces for Javascript/Typescript uses readline() and print() functions for input and output to the standard input/console.

How to use

This library exposes the functions in a manner that allows you to just copy the source code for the submission.

Importing the functions

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');

readline()

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
}

Special mention

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();
}

print()

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);

testOutput()

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.

console

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.

Code example

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

Solution generator

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 using cf in the command line.

Parameters

  • 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 to index.
  • --js - Uses .js extension instead of .ts
  • --cjs - Uses require instead of ES6 import/export
  • --c or --comment - Adds a comment at the beginning of the file. E.g. the link to the problem.

Usage examples

  • npx @ip-algorithmics/codeforces-io ./ProblemA --js --cjs
  • cf ./ProblemA --js --cjs
  • cf ./ProblemA
  • cf ./ProblemA --c http://link.to.problem

Typescript ideas

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 like npm run build <path>
  • manually run tsc --module es6 --moduleResolution node <path>

Future plans

  • 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

Additional Resources

CodeForces Solutions Codeforces - How to use Typescript/Javascript like a pro

Changelog

1.1.1

Added solution generator capabilities.

1.0.12

Added testOutput() function.