Hướng dẫn fast input in python
Hello CodeForces Community, Show Input / Output in Python can be sometimes time taking in cases when the input is huge or we have to output many number of lines, or a huge number of arrays(lists) line after line. I have come across many questions on CodeForces where the style of taking input and printing makes your code quite faster. For Input :- Normally, the input is taken from STDIN in the form of String using The code for that would be :-
or a native method (which can be used for not too large inputs) :-
Now for Output :- Instead of throwing the output to the STDOUT, we can try writing to the Judge's sytem file. The code for that would be to use
Examples :- For printing an integer, instead of
Use :-
For printing a list of integers, instead of
Use :-
Now Program examples On CodeForces, where this was useful :- Question
1 Question 2 View Discussion Improve Article Save Article View Discussion Improve Article Save Article In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line. Fast InputNormally, the input is taken from STDIN in the form of String using input(). And this STDIN is provided in the Judge’s file. So try reading the input directly from the Judge’s file using the Operating system(os) module, and input/output (io) module. This reading can be done in the form of bytes. By using this method, integer input works normally, but for string input, it will store the string as a byte like an object. For correcting this, the string can be decoded using the decode function. Below is the implementation for Fast I/O in Python: Python3
Output: Fast OutputInstead of outputting to the STDOUT, we can try writing to the Judge’s system file. The code for that would be to use sys.stdout.write() instead of print() in Python. But remember we can only output strings using this, so convert the output to a string using str() or map(). Below is the implementation for the Fast Output: Python3
Output: |