SPOJ ? Test for end of file input

Discussion in 'Ruby' started by Aldric Giacomoni, Jan 12, 2010.

  1. 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?
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Jan 12, 2010
    #1
    1. Advertising

  2. Aldric Giacomoni 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.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Jan 12, 2010
    #2
    1. Advertising

  3. Aldric Giacomoni 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.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 12, 2010
    #3
  4. Brian Candler 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!
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Jan 12, 2010
    #4
  5. Aldric Giacomoni

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    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.
    Josh Cheek, Jan 12, 2010
    #5
  6. 2010/1/12 Aldric Giacomoni <>:
    > Brian Candler wrote:
    >>
    >> gets will return nil on end of file.
    >>
    >> while line =3D $stdin.gets
    >> =A0 line.chomp!
    >> =A0 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

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 13, 2010
    #6
  7. Robert Klemme 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'.

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jan 13, 2010
    #7
  8. 2010/1/13 Brian Candler <>:
    > Robert Klemme wrote:
    >> I would do
    >>
    >> $stdin.each do |line|
    >> =A0 line.chomp!
    >> =A0 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'.


    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

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jan 13, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shriphani
    Replies:
    2
    Views:
    549
    Ian Kelly
    Jun 1, 2008
  2. bert

    Re: PRIME1 (SPOJ)

    bert, Oct 18, 2010, in forum: C Programming
    Replies:
    4
    Views:
    441
    Michael Foukarakis
    Oct 20, 2010
  3. Ben Bacarisse

    Re: PRIME1 (SPOJ)

    Ben Bacarisse, Oct 19, 2010, in forum: C Programming
    Replies:
    2
    Views:
    426
    Ben Bacarisse
    Oct 19, 2010
  4. BartC

    Re: PRIME1 (SPOJ)

    BartC, Oct 19, 2010, in forum: C Programming
    Replies:
    5
    Views:
    508
    BartC
    Oct 19, 2010
  5. Ben Bacarisse

    Re: PRIME1 (SPOJ)

    Ben Bacarisse, Oct 19, 2010, in forum: C Programming
    Replies:
    8
    Views:
    535
    Dann Corbit
    Oct 20, 2010
Loading...

Share This Page