[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

single to multi dimension array conversion

Ashikali Ashikali

4/13/2009 10:58:00 AM

Can any one tell me converting single to multi dimension array
conversion

For example ,
a = [ 1,2,3,4,5,6,7,8,9,10 ]
a.<methodName>( 2,3 )

Output,



here ,
2 means dimension No
3 means elements in each dimension
--
Posted via http://www.ruby-....

3 Answers

Ashikali Ashikali

4/13/2009 11:00:00 AM

0

Ashikali Ashikali wrote:
> Can any one tell me converting single to multi dimension array
> conversion
>
> For example ,
> a = [ 1,2,3,4,5,6,7,8,9,10 ]
> a.<methodName>( 2,3 )
>
> Output,
>
>
>
> here ,
> 2 means dimension No
> 3 means elements in each dimension


In above example output should be ,
a = [ [1,2,3],[4,5,6],[7,8,9] , [ 10 ] ]
--
Posted via http://www.ruby-....

James Coglan

4/13/2009 11:25:00 AM

0

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

2009/4/13 Ashikali Ashikali <ashikali.m@gmail.com>

> Ashikali Ashikali wrote:
> > Can any one tell me converting single to multi dimension array
> > conversion
> >
> > For example ,
> > a = [ 1,2,3,4,5,6,7,8,9,10 ]
> > a.<methodName>( 2,3 )
> >
> > Output,
> >
> >
> >
> > here ,
> > 2 means dimension No
> > 3 means elements in each dimension
>
>
> In above example output should be ,
> a = [ [1,2,3],[4,5,6],[7,8,9] , [ 10 ] ]


That has more than three elements along the outermost direction. It
represents the matrix:

1 2 3
4 5 6
7 8 9
10

So you can't fit the data into a 3x3 space. You might need to specify the
problem a bit more carefully.

James Coglan

4/13/2009 11:46:00 AM

0

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

2009/4/13 James Coglan <jcoglan@googlemail.com>

> 2009/4/13 Ashikali Ashikali <ashikali.m@gmail.com>
>
> > Ashikali Ashikali wrote:
> > > Can any one tell me converting single to multi dimension array
> > > conversion
> > >
> > > For example ,
> > > a = [ 1,2,3,4,5,6,7,8,9,10 ]
> > > a.<methodName>( 2,3 )
> > >
> > > Output,
> > >
> > >
> > >
> > > here ,
> > > 2 means dimension No
> > > 3 means elements in each dimension
> >
> >
> > In above example output should be ,
> > a = [ [1,2,3],[4,5,6],[7,8,9] , [ 10 ] ]
>
>
> That has more than three elements along the outermost direction. It
> represents the matrix:
>
> 1 2 3
> 4 5 6
> 7 8 9
> 10
>

Although, here's one possible and slightly more general solution:

class Array
def multidim(*sizes)
split = sizes.inject { |a,b| a * b }
return self unless split
result = []
each_slice(split) { |slice| result << slice.multidim(*sizes[1..-1]) }
result
end
end

With this, you specify how many you want in each dimension, so:

[1,2,3,4,5,6,7,8,9,10].multidim(3)
#=> [[1,2,3],[4,5,6],[7,8,9],[10]]

It works from the outside in, so for higher dimensions it works like this:

[1,2,3,4,5,6,7,8,9,10].multidim(3,2)
#=> [ [ [1, 2],
[3, 4],
[5, 6]
],
[ [7, 8],
[9, 10]
] ]