Nice "bug" to loose a contest

stdazi stdazi at
Sun Apr 8 12:52:08 CEST 2007


Yesterday, I was at a programming competition. We programmed on Linux
liveCD's and Python was one of the allowed languages (among C and
Java). I cared just about the algorithmic approach so I used Python.
One of the main rules is, that the code reads its standard input and
dumps the result on the standard output. Here happened my bigger
programming mistake ever - I used the following approach :

import sys
print sys.stdout.readlines() # TYPO ! stdin != stdout

So the WEIRD issue is, that it worked and, executing the following
code I get :

azi at magicb0x ~ $ python
test #2
['test \n', 'test #2\n']
azi at magicb0x ~ $

Ok, as the algorithm worked perfectly, and all the test cases we were
given were positive, I've continued with the next problem, copy/
pasting the above idiom... When they tested the code, they used file
redirection like :

python < /somefile

And, my code resulted in no output/input (=> 0 points), which can be
proved here too :

azi at magicb0x ~ $ python < /etc/passwd


Some friend of mine told me that's the Unix way, (stdout can act like
stdin) so I tried some C code :

  1 #include <stdio.h>
  3 int main() {
  4     char buf[120];
  5     while (fgets(buf, sizeof(buf), stdout) != NULL) {
  6         puts(buf);
  7     }
  8    return 0;

The code returns with no delay, so I'm really wondering where is that
nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
anything. I'd spot the mistake before submitting the problem solutions
if it was written in C :)


More information about the Python-list mailing list