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.
Normally, 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:
Instead 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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course