SPOJ ? Test for end of file input

Here’s the problem:
https://www.spoj.pl/problems/PROBTNPO/

It’s pretty trivial, but it doesn’t have a well-delimited “end” to it.
In the forums, they indicate one should test for end of file. I have no
idea how to do that in Ruby.
I mean - I paste my code in a box, tell it to run and then they tell me
if it worked or not.
I can only suppose they’re using the “-i” argument … If so, how do I
test for when that file has run out of stuff?

Aldric G. wrote:

I can only suppose they’re using the “-i” argument … If so, how do I
test for when that file has run out of stuff?

Well, with some research on the command-line arguments, I’ve now shown
my complete ignorance in the subject. I’m at square… zero, or -1, I
think.

Aldric G. wrote:

It’s pretty trivial, but it doesn’t have a well-delimited “end” to it.
In the forums, they indicate one should test for end of file. I have no
idea how to do that in Ruby.

gets will return nil on end of file.

while line = $stdin.gets
line.chomp!
puts “I got #{line} !!”
end

I can only suppose they’re using the “-i” argument … If so, how do I
test for when that file has run out of stuff?

huh? -i is “in-place edit mode”. It makes ruby read in one or more files
(whose names are listed on the command line), and write them back out
again with modifications under the original names, optionally renaming
the original file to a different name. You’d use it if you were using
ruby to do search-and-replace type operations.

Brian C. wrote:

gets will return nil on end of file.

while line = $stdin.gets
line.chomp!
puts “I got #{line} !!”
end

Ah! And the light comes on and blinds me. Thank you!

Ugh, sat down and played with it, and now I remember why I gave up on
SPOJ,
they tell you you got it wrong, but don’t tell you why.

It usually boils down to obscure undocumented output formatting
differences
(ie how much whitespace and where), but you can’t tell whether it’s due
to
formatting error, or an actual wrong answer.

SPOJ needs to give feedback a la javabat.com

Also, codechef.com is basically SPOJ with a prettier interface, active
administration, and a community behind it. But I have had difficulty
getting
Ruby to work there, in the past. Not sure why.

2010/1/12 Aldric G. [email protected]:

Brian C. wrote:

gets will return nil on end of file.

while line = $stdin.gets
line.chomp!
puts “I got #{line} !!”
end

Ah! And the light comes on and blinds me. Thank you!

I would do

$stdin.each do |line|
line.chomp!
puts “I got #{line} !!”
end

because that is the more Ruby typical idiom for iterating all lines.

Kind regards

robert

2010/1/13 Brian C. [email protected]:

Use #each_line if you want to do that. Ruby 1.9 removed #each from
Strings, but strangely decided to leave it on Files. To be safe, don’t
assume that ‘each’ means ‘each line’.

Hmm, both #each and #each_line work on 1.8.7 and 1.9.1. But you’re
right, #each_line is more to the point. Thanks for the heads up,
Brian!

Cheers

robert

Robert K. wrote:

I would do

$stdin.each do |line|
line.chomp!
puts “I got #{line} !!”
end

because that is the more Ruby typical idiom for iterating all lines.

Use #each_line if you want to do that. Ruby 1.9 removed #each from
Strings, but strangely decided to leave it on Files. To be safe, don’t
assume that ‘each’ means ‘each line’.