Today is pretty much like another day, I choose to grind some codeforces problem here and there... I choose to solve this problem today: Grid XOR
It so happens I wrote a solution and tested on local, works perfectly fine. But when I submitted OJ tell me it's runtime error on test 1 (?)
This never happened to me before so I'm confused. So I happen to submitted mutliple bad subs to test what actually happened, and this is what I've found: Compared this 2 submission: 314967876 vs 314969708
The only difference is this line of code, and it causes RTE:
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
What happened to python fastIO input? Just pretty much yesterday everything works fine and now it doesn't? I tried to scramble blogs updated from yesterday till today and found nothing.
Can anyone fill me up what's happening?








UPD: faster input with different way would work 314971286.
But still I want to know why the old
io.BytesIOhas stopped working...The issue has to do with that problem being interactive (as in the input is not given from a file). The command
io.BytesIO(os.read(0, os.fstat(0).st_size)).readlineassumes there is a file to read, whereos.fstat(0).st_sizeis the number of bytes of the file.I would recommend just using
sys.stdin.buffer.readline(orsys.stdin.readline). It will run fast and work on any problem.Historically PyPy's io was slow, which is the reason people started using things like
io.BytesIO(os.read(0, os.fstat(0).st_size)).readline. But this has been fixed since many years ago. Nowdays there is little reason to useio.BytesIO(os.read(0, os.fstat(0).st_size)).readline.Thanks a lot, I was completely missing this problem is actually interactive (because of very normal input/output format) thus breaking the
io.BytesIO(os.read(0, os.fstat(0).st_size)).readlineI'll update my code template and avoid these funny bugs in the future.
Which problems are "interactive" or not can be quite deceiving. I know of problems that are fundamentally non-interactive but that are set as interactive problems simply because the problemsetters found that convenient. Because of this, in general it is bad to rely on code that assumes the input is given as a file.
Examples of ways in Python to read input that are broken for "interactive" problems are
io.BytesIO(os.read(0, os.fstat(0).st_size)).readlinesys.stdin.read().split()[::-1].popsys.stdin.buffer.read().split()[::-1].popAlternatives of "fast input" that don't have this issue are
sys.stdin.buffer.readlineor
sys.stdin.readline