SyHoMadara's blog

By SyHoMadara, 4 years ago, In English

I was thinking on this problem Taxi Problem and it was not solved then I see the smallest code that was written in python 3 it was this :

a=[*open(0)][1].count;print(a('4')+a('3')+(a('2')*2+max(0,a('1')-a('3'))+3)//4)

So i could not understand what is [*open(0)] that was used and after I run it in my laptop it return

Traceback (most recent call last):

File "/home/hosein/Python/test.py", line 1, in <module>

a=[*open(0)][1].count;print(a('4')+a('3')+(a('2')*2+max(0,a('1')-a('3'))+3)//4)

IndexError: list index out of range

but when i submit it, it was Accepted whats the point of this?

  • Vote: I like it
  • -18
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

If you read the documentation carefully, you can see that the file argument can also be an integer representing the file descriptor. And the number 0 here is the file descriptor for stdin. So open(0) as far as I know is just sys.stdin. And since it is a file, it is also iterable, which has the spread operator (the star before open(0)). So [*open(0)] is telling python to put every line of stdin into a list.