Forum: Ruby Logic...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E50d89fffb1f64ceb5d752760fc3f91a?d=identicon&s=25 StarLion (Guest)
on 2005-12-10 18:25
def MakeBoard
 rcount = 0
 puts "Push enter, then begin input. (For Input Format Compliance, extra
enter key is necessary)"
 gets
 board = [[],[],[],[],[],[],[],[],[]]
 9.times do
   instr = gets
   board[rcount] =
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
   rcount += 1
 puts board[rcount]
 end #do
 return board
end #MakeBoard

Is my logic flawed here? the puts doesnt actually output anything.
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 lopexx (Guest)
on 2005-12-10 18:39
(Received via mailing list)
StarLion wrote:
>
> Is my logic flawed here? the puts doesnt actually output anything.
>

It is supposed not to print anything since nothing is called here...

just call the MakeBoard method

lopex
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 m.fellinger (Guest)
on 2005-12-10 18:43
(Received via mailing list)
Ok, i made a bit more cleaned up version, but i'm still not sure what
you want
to do with your method.
especially, what does this .to_i mean?

However, for everything you write twice or more often, ruby usually has
a way
to make it at the same time cleaner and easier to write.


def make_board
  puts "Push enter, then begin input."
  puts "(For Input Format Compilance, extra enter key is necessary)"
  gets
  board = Array.new(9){[]}
  board.size.times do |i|
    instr = gets
    board[i] = instr.split(//).map{|c| c.to_i}
    puts board[i]
  end
  return board
end

Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-10 18:43
(Received via mailing list)
StarLion wrote:
>    rcount += 1
>  puts board[rcount]
>  end #do
>  return board
> end #MakeBoard
>
> Is my logic flawed here? the puts doesnt actually output anything.

I think it's because when you write `puts board[rcount]', `rcount' is 1
higher than the highest index number in board. Try this instead.

   def make_board
     puts "Push enter, then begin input. (For Input Format Compliance,
extra enter key is necessary)"
     gets
     board = [[],[],[],[],[],[],[],[],[]]
     9.times do |i|
       # you may want to use gets.chomp instead
       instr = gets
       board[i] =
[instr[0].to_i,instr[1].to_i,instr[2].to_i,instr[3].to_i,instr[4].to_i,instr[5].to_i,instr[6].to_i,instr[7].to_i,instr[8].to_i]
       puts board[i] # the last item in `board'
     end
     return board
   end

Cheers,
Daniel
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-12-10 18:51
(Received via mailing list)
On 12/10/05, StarLion <hmrhouse@hotmail.com> wrote:
>    rcount += 1
>  puts board[rcount]
>  end #do
>  return board
> end #MakeBoard
>
> Is my logic flawed here? the puts doesnt actually output anything.

I think this is a little cleaner:

make_board returns a board split by spaces.

i.e. you enter the rows like

1 0 1 0 2 1 8 2 1
1 2 1 1 9 1 9 1 9
...

The method returns the actual array, and I map and join for pretty
printing in the puts.
Tweak as needed, code below.
------------------------------------------------

def make_board
  board = []
  puts "input rows, seperated by spaces:"
  9.times { board << gets.split(" ")[0..8].map { |e| e.to_i } }
  return board
end

puts make_board.map { |r| r.join(" ") }.join("\n")
E50d89fffb1f64ceb5d752760fc3f91a?d=identicon&s=25 StarLion <hmrhouse@hotmail.com> (Guest)
on 2005-12-10 18:55
m.fellinger wrote:
> Ok, i made a bit more cleaned up version, but i'm still not sure what
> you want
> to do with your method.
> especially, what does this .to_i mean?
>
> However, for everything you write twice or more often, ruby usually has
> a way
> to make it at the same time cleaner and easier to write.
>
>
> def make_board
>   puts "Push enter, then begin input."
>   puts "(For Input Format Compilance, extra enter key is necessary)"
>   gets
>   board = Array.new(9){[]}
>   board.size.times do |i|
>     instr = gets
>     board[i] = instr.split(//).map{|c| c.to_i}
>     puts board[i]
>   end
>   return board
> end
>
> Am Samstag, 10. Dezember 2005 18:25 schrieb StarLion:

The idea is to take a string of 9 characters, and transform them into an
array of integers, which are stored in an array of rows (IE: generate a
9x9 array.).
E50d89fffb1f64ceb5d752760fc3f91a?d=identicon&s=25 StarLion <hmrhouse@hotmail.com> <hmrhouse@hotmail. (Guest)
on 2005-12-10 18:56
> The idea is to take a string of 9 characters, and transform them into an
> array of integers, which are stored in an array of rows (IE: generate a
> 9x9 array.).

Obviously i meant 9 strings of 9 characters.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-12-10 19:07
(Received via mailing list)
On 12/10/05, StarLion <hmrhouse@hotmail.com> <hmrhouse@hotmail.
<hmrhouse@hotmail.com> wrote:
> > The idea is to take a string of 9 characters, and transform them into an
> > array of integers, which are stored in an array of rows (IE: generate a
> > 9x9 array.).
>
> Obviously i meant 9 strings of 9 characters.

That is what my code does, but it seperates by spaces to make it
easier to see as your typing in... replace the split(" ") with
split(//) and it will do exactly that

rows can then be entered like this: 123456789
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 m.fellinger (Guest)
on 2005-12-10 19:07
(Received via mailing list)
i see.. it wasn't clear at first what the kind of data is that is the
input
for the program.
However, after testing my little suggestion, i found that there are is a
bug
in it - it didn't strip the \n from the line it gets. also i made the
iteration independent from the size of the board and made the output
each
line a bit prettier ^^

def make_board
  puts "Push enter, then begin input."
  puts "(For Input Format Compilance, extra enter key is necessary)"
  gets
  board = Array.new(9){[]}
  9.times do |i|
    instr = gets.strip
    board[i] = instr.split(//).map{|c| c.to_i}
    puts board[i].inspect
  end
  return board
end

Am Samstag, 10. Dezember 2005 18:56 schrieb StarLion
<hmrhouse@hotmail.com>
<hmrhouse@hotmail.:
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-12-10 19:11
(Received via mailing list)
On 12/10/05, Michael Fellinger <m.fellinger@gmail.com> wrote:
> i see.. it wasn't clear at first what the kind of data is that is the input
> for the program.
> However, after testing my little suggestion, i found that there are is a bug
> in it - it didn't strip the \n from the line it gets. also i made the
> iteration independent from the size of the board and made the output each
> line a bit prettier ^^

if you use a range, you don't need to worry about extraneous input
(such as the \n)

def make_board
 board = []
 puts "input rows, seperated by spaces:"
 9.times { board << gets.split(//)[0..8].map { |e| e.to_i } }
 return board
end

puts make_board.map { |r| r.join(" ") }.join("\n")
E50d89fffb1f64ceb5d752760fc3f91a?d=identicon&s=25 StarLion (Guest)
on 2005-12-10 22:51
> def make_board
>   puts "Push enter, then begin input."
>   puts "(For Input Format Compilance, extra enter key is necessary)"
>   gets
>   board = Array.new(9){[]}
>   9.times do |i|
>     instr = gets.strip
>     board[i] = instr.split(//).map{|c| c.to_i}
>     puts board[i].inspect
>   end
>   return board
> end
>

This code returns a 10 element array, with 0 in index 9.
E50d89fffb1f64ceb5d752760fc3f91a?d=identicon&s=25 StarLion (Guest)
on 2005-12-10 22:52
>
> This code returns a 10 element array, with 0 in index 9.

Nevermind... I just cant count how many characters i put on one line.
-_-
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-10 23:03
(Received via mailing list)
On Dec 10, 2005, at 3:52 PM, StarLion wrote:

>
>>
>> This code returns a 10 element array, with 0 in index 9.
>
> Nevermind... I just cant count how many characters i put on one line.

Which is probably a sign that input error checking is needed:

input =~ /^\d{9}$/

James Edward Gray II
This topic is locked and can not be replied to.