[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to un-ragged a 2D array?

Phlip

3/7/2009 4:19:00 PM

Rubies:

Here's a ragged array:

[ [ 1 ],
[ 2, 3 ],
[ 4 ],
[ 5, 6 ],
[ 7 ],
[ 8 ] ]

How, with the tightest, or most modern, or coolest statements, can we turn it
into this?

[ [ 1, 1 ],
[ 2, 3 ],
[ 4, 4 ],
[ 5, 6 ],
[ 7, 7 ],
[ 8, 8 ] ]

--
Phlip
http://www.zerop...
11 Answers

Martin DeMello

3/7/2009 5:12:00 PM

0

On Sat, Mar 7, 2009 at 9:48 PM, Phlip <phlip2005@gmail.com> wrote:
> Rubies:
>
> Here's a ragged array:
>
> =A0[ [ 1 =A0 =A0],
> =A0 [ 2, 3 ],
> =A0 [ 4 =A0 =A0],
> =A0 [ 5, 6 ],
> =A0 [ 7 =A0 =A0],
> =A0 [ 8 =A0 =A0] ]
>
> How, with the tightest, or most modern, or coolest statements, can we tur=
n
> it into this?
>
> =A0[ [ 1, 1 ],
> =A0 [ 2, 3 ],
> =A0 [ 4, 4 ],
> =A0 [ 5, 6 ],
> =A0 [ 7, 7 ],
> =A0 [ 8, 8 ] ]

a.each {|i| i[1] ||=3D i[0]}

martin

Sebastian Hungerecker

3/7/2009 5:13:00 PM

0

Phlip wrote:
> Here's a ragged array:
>
> =A0 [ [ 1 =A0 =A0],
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 [ 4 =A0 =A0],
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 [ 7 =A0 =A0],
> =A0 =A0 [ 8 =A0 =A0] ]
>
> How, with the tightest, or most modern, or coolest statements, can we turn
> it into this?
>
> =A0 [ [ 1, 1 ],
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 [ 4, 4 ],
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 [ 7, 7 ],
> =A0 =A0 [ 8, 8 ] ]

I'm making the following assumptions:
a) we have variable "size" which contains the size of a full subarray
b) subarrays should be filled by repeating the last element until the subar=
ray
is full.

array =3D [ [ 1 ],
[ 2, 3 ],
[ 4 ],
[ 5, 6 ],
[ 7 ],
[ 8 ] ]
size =3D 2
array.map do |subarray|
subarray + [subarray.last] * (size - subarray.size)
end
# =3D> [ [1, 1],
[2, 3],
[4, 4],
[5, 6],
[7, 7],
[8, 8] ]

HTH,
Sebastian

Phlip

3/7/2009 10:11:00 PM

0

Ktx! Now, on to Round Two!

What if the array were even _more_ ragged??

[ 1 ,
[ 2, 3 ],
4 ,
[ 5, 6 ],
7 ,
8 ]

The back-story here is: We are asking the user-programmer to enter either items
or couplets of items. Consider the ActiveRecord DSL, where an :include => item,
for example, can be a single item, or an array of items, or a hash of paired
items, or a hash pointing to an array of items, and so on. AR _could_ interpret
:include => item by promoting all its scalars to arrays, then running a simpler
algorithm that only expects arrays.

In my case, the user-programmer can leave the second 1, 4, 7, & 8 out, as a
convenience, but the algorithm wants this...

[ [ 1, 1 ],
[ 2, 3 ],
[ 4, 4 ],
[ 5, 6 ],
[ 7, 7 ],
[ 8, 8 ] ]

....as an internal convenience.

My attempt is needs = needs.map{|x| x.is_a?(Array) ? x : [x,x] }, which is truly
icky, but might be the shortest way. Besides this!

needs = needs.map{|x| [x,x].flatten[0..1] }

--
Phlip

Martin DeMello

3/7/2009 10:46:00 PM

0

On Sun, Mar 8, 2009 at 3:43 AM, Phlip <phlip2005@gmail.com> wrote:
> Ktx! Now, on to Round Two!
>
> What if the array were even _more_ ragged??
>
> =A0 [ =A0 1 =A0 =A0 ,
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 =A0 4 =A0 =A0 ,
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 =A0 7 =A0 =A0 ,
> =A0 =A0 =A0 8 =A0 =A0 =A0]

class Fixnum
def to_a
[self, self]
end
end

:)

martin

Phlip

3/8/2009 12:06:00 AM

0

Martin DeMello wrote:

>> What if the array were even _more_ ragged??
>>
>> [ 1 ,
>> [ 2, 3 ],
>> 4 ,
>> [ 5, 6 ],
>> 7 ,
>> 8 ]
>
> class Fixnum
> def to_a
> [self, self]
> end
> end
>
> :)

That thing with the colon and paren, is it Ruby's operator to
turn-this-monkey-patch-off-if-Fixnum-gets-reused-in-other-modules,
mebbe?

--
Phlip

Michael Fellinger

3/8/2009 12:23:00 AM

0

On Sun, Mar 8, 2009 at 7:13 AM, Phlip <phlip2005@gmail.com> wrote:
> Ktx! Now, on to Round Two!
>
> What if the array were even _more_ ragged??
>
> =C2=A0 [ =C2=A0 1 =C2=A0 =C2=A0 ,
> =C2=A0 =C2=A0 [ 2, 3 ],
> =C2=A0 =C2=A0 =C2=A0 4 =C2=A0 =C2=A0 ,
> =C2=A0 =C2=A0 [ 5, 6 ],
> =C2=A0 =C2=A0 =C2=A0 7 =C2=A0 =C2=A0 ,
> =C2=A0 =C2=A0 =C2=A0 8 =C2=A0 =C2=A0 =C2=A0]
>
> The back-story here is: We are asking the user-programmer to enter either
> items or couplets of items. Consider the ActiveRecord DSL, where an :incl=
ude
> =3D> item, for example, can be a single item, or an array of items, or a =
hash
> of paired items, or a hash pointing to an array of items, and so on. AR
> _could_ interpret :include =3D> item by promoting all its scalars to arra=
ys,
> then running a simpler algorithm that only expects arrays.
>
> In my case, the user-programmer can leave the second 1, 4, 7, & 8 out, as=
a
> convenience, but the algorithm wants this...
>
> =C2=A0 [ [ 1, 1 ],
> =C2=A0 =C2=A0 [ 2, 3 ],
> =C2=A0 =C2=A0 [ 4, 4 ],
> =C2=A0 =C2=A0 [ 5, 6 ],
> =C2=A0 =C2=A0 [ 7, 7 ],
> =C2=A0 =C2=A0 [ 8, 8 ] ]
>
> ...as an internal convenience.
>
> My attempt is needs =3D needs.map{|x| x.is_a?(Array) ? x : [x,x] }, which=
is
> truly icky, but might be the shortest way. Besides this!
>
> =C2=A0 =C2=A0 =C2=A0needs =3D needs.map{|x| [x,x].flatten[0..1] }
>
> --
> =C2=A0Phlip
>
>

needs.map{|e| ([*e] * 2).first(2) }

^ manveru

Martin DeMello

3/8/2009 12:55:00 AM

0

On Sun, Mar 8, 2009 at 5:38 AM, Phlip <phlip2005@gmail.com> wrote:
> Martin DeMello wrote:
>>
>> class Fixnum
>> =A0def to_a
>> =A0 =A0 [self, self]
>> =A0end
>> end
>>
>> :)
>
> That thing with the colon and paren, is it Ruby's operator to
> turn-this-monkey-patch-off-if-Fixnum-gets-reused-in-other-modules,
> mebbe?

Indeed (:

martin

Phlip

3/8/2009 1:08:00 AM

0

>> needs.map{|x| [x,x].flatten[0..1] }

> needs.map{|e| ([*e] * 2).first(2) }

Look, ma! No .flatten!

That generally might flatten things that deserve to be shapely... (or ragged;).

lasitha

3/8/2009 2:59:00 PM

0

On Sun, Mar 8, 2009 at 3:43 AM, Phlip <phlip2005@gmail.com> wrote:
> Ktx! Now, on to Round Two!
>
> What if the array were even _more_ ragged??
>
> =A0 [ =A0 1 =A0 =A0 ,
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 =A0 4 =A0 =A0 ,
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 =A0 7 =A0 =A0 ,
> =A0 =A0 =A0 8 =A0 =A0 =A0]
>
> <snip>
>
> In my case, the user-programmer can leave the second 1, 4, 7, & 8 out, as=
a
> convenience, but the algorithm wants this...
>
> =A0 [ [ 1, 1 ],
> =A0 =A0 [ 2, 3 ],
> =A0 =A0 [ 4, 4 ],
> =A0 =A0 [ 5, 6 ],
> =A0 =A0 [ 7, 7 ],
> =A0 =A0 [ 8, 8 ] ]
>

needs.map {|x, y| [x, y ||=3D x] }

Solidarity,
lasitha.

Phlip

3/8/2009 5:09:00 PM

0

lasitha wrote:

> needs.map {|x, y| [x, y ||= x] }

You win! But try just:

needs.map{|x, y| [x, y || x] }

(And would needs.map{|*x| [*x, *x].first(2) } work on Ruby 1.9? 1.8.7?)

However, I ain't gonna edit my gist just to put it in - you missed the deadline! (-:

http://gist.github...

--
Phlip