Module io

Module io 

Source
Expand description

This module features utilities for reading input using the Scanner, and writing output using the Writer and wln macro.

§Examples

You will typically use the test_cases() or test_case() functions, to obtain a Scanner and a Writer when solving contest problems.

§Reading input of a single test

It is less common nowadays, as most contest will provide you with several test cases, but you can still do it, if you need to:

# Input:
2 3

# Output:
Sum: 5
use algorist::io::{test_case, wln};

fn main() {
    test_case(&mut |scan, w| {
        let (a, b): (i32, i32) = scan.pair();
        wln!(w, "Sum: {}", a + b);
    });
}

§Reading number of test cases and processing them

Normally, in contest programming, you will have several test cases to process.

So, the input will start with a single integer t, which is the number of test cases, followed by t test case inputs.

# Input:
2
3 2
1 2

# Output:
Sum: 5
Sum: 3
use algorist::io::{test_cases, wln};

fn main() {
    test_cases(&mut |scan, w| {
        let (a, b): (i32, i32) = scan.pair();
        wln!(w, "Sum: {}", a + b);
    });
}

As you can see, the difference between reading a single test case and reading multiple test cases is minimal – you just need to call different function, with the same closure.

Macros§

wln
A macro for writing a line with formatted output.

Structs§

Scanner
Scanner reads buffered input and parses it into tokens.
Writer
A Writer is a wrapper around BufWriter<W> that provides a convenient interface for writing formatted output, without requiring to import std::io::Write by the client code. It is expected to be used with wln! macro.

Functions§

test_case
A helper function to read a single test case from standard input, and write to standard output.
test_cases
A helper function to read multiple test cases from standard input, and write output to standard output.
wvln