Convert singel dimention array into grid

G

Giant Cranes

Hi,

I have been learning ruby for the past week and am completely hooked.
Most of the code I am producing is elegant (ruby's credit), although I
am having trouble elegantly converting a single dimension array with 16
elements into a two-dimension grid [4x4].

Here is what I have so far:

@array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
@grid = Array.new

i = 0
index = 0
0.upto(3) do |i|
j = 0
0.upto(3) do |j|
@grid = Array.new if j == 0
@grid[j] = @array[index]
index += 1
j += 1
end
i += 1
end

@grid

Thanks,
Giant Cranes
 
P

Peter Szinek

Giant said:
Hi,

I have been learning ruby for the past week and am completely hooked.
Most of the code I am producing is elegant (ruby's credit), although I
am having trouble elegantly converting a single dimension array with 16
elements into a two-dimension grid [4x4].

require 'enumerator'
@array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

result = []
@array.each_slice(4) {|s| result << s }

should do the job.

Cheers,
Peter

__
http://www.rubyrailways.com
 
D

dblack

Hi --

Hi,

I have been learning ruby for the past week and am completely hooked.
Most of the code I am producing is elegant (ruby's credit), although I
am having trouble elegantly converting a single dimension array with 16
elements into a two-dimension grid [4x4].

Here is what I have so far:

@array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
@grid = Array.new

i = 0
index = 0
0.upto(3) do |i|
j = 0
0.upto(3) do |j|
@grid = Array.new if j == 0
@grid[j] = @array[index]
index += 1
j += 1
end
i += 1
end

@grid


You can do:

require 'enumerator'
@array = [*1..16] # :)
@grid = *@array.enum_slice(4)

Enumerators are, in my view, a bit less transparent than most Ruby
constructs, so this may look more cryptic than it's worth. The basic
idea is that you're creating an object -- an Enumerator -- that has no
real data of its own but that represents, so to speak, the possibility
of an enumeration over @array, sliced up into groups of four. Instead
of enumerating, I've converted it to a plain array with the *
operator.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
T

Tim Pease

Hi,

I have been learning ruby for the past week and am completely hooked.
Most of the code I am producing is elegant (ruby's credit), although I
am having trouble elegantly converting a single dimension array with 16
elements into a two-dimension grid [4x4].

Here is what I have so far:

@array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
@grid = Array.new

i = 0
index = 0
0.upto(3) do |i|
j = 0
0.upto(3) do |j|
@grid = Array.new if j == 0
@grid[j] = @array[index]
index += 1
j += 1
end
i += 1
end

@grid


Let the golfing commence!

require 'enumerator'

@array = (1..16).to_a
@grid = Array.new(4) {|idx| Array.new(4)}

@array.each_with_index do |val, idx|
@grid[idx/4][idx%4] = val
end


Blessings,
TwP
 
D

dblack

Hi --

Hi,

I have been learning ruby for the past week and am completely hooked.
Most of the code I am producing is elegant (ruby's credit), although I
am having trouble elegantly converting a single dimension array with 16
elements into a two-dimension grid [4x4].

Here is what I have so far:

@array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
@grid = Array.new

i = 0
index = 0
0.upto(3) do |i|
j = 0
0.upto(3) do |j|
@grid = Array.new if j == 0
@grid[j] = @array[index]
index += 1
j += 1
end
i += 1
end

@grid


Let the golfing commence!

require 'enumerator'

@array = (1..16).to_a
@grid = Array.new(4) {|idx| Array.new(4)}

@array.each_with_index do |val, idx|
@grid[idx/4][idx%4] = val
end


Well, if we're golfing:

require'enumerator'
g=*(1..16).enum_slice(4)


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,727
Latest member
PasqualePf

Latest Threads

Top