Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

akane646's blog

By akane646, history, 19 months ago, In English

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?

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
19 months ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

UPD: faster input with different way would work 314971286.

input = lambda: sys.stdin.readline().rstrip()

But still I want to know why the old io.BytesIO has stopped working...

»
19 months ago, hide # |
 
Vote: I like it +15 Vote: I do not like it

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)).readline assumes there is a file to read, where os.fstat(0).st_size is the number of bytes of the file.

I would recommend just using sys.stdin.buffer.readline (or sys.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 use io.BytesIO(os.read(0, os.fstat(0).st_size)).readline.

  • »
    »
    19 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    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)).readline

    I'll update my code template and avoid these funny bugs in the future.

    • »
      »
      »
      19 months ago, hide # ^ |
      ← Rev. 3  
      Vote: I like it +5 Vote: I do not like it

      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)).readline

      sys.stdin.read().split()[::-1].pop

      sys.stdin.buffer.read().split()[::-1].pop

      Alternatives of "fast input" that don't have this issue are

      sys.stdin.buffer.readline

      or

      sys.stdin.readline