[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array Practice

Adam Akhtar

2/6/2008 11:21:00 PM

As some of you may know from previous threads im trying to practice
specific areas of ruby. One simple exercise i set myself was taking a
elements in an array such as

[1,2,3,1,2,2,4,3,2,1]

and then grouping them together and putting them in subarrays within an
array like this

[ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]

Heres how i did it (im new to both programming and ruby)

def subpack(list)
uniqlist = Array.new
subpacklist = Array.new
list.sort!

uniqlist = list.uniq
uniqlist.each_index do |i|
rangeleft = list.index(uniqlist[i])
rangeright = list.rindex(uniqlist[i])
subpacklist << list.slice(rangeleft..rangeright)
end
subpacklist
end


WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
i.e. give me some hints, the names of some methods but not the solution
as i want a go myself.
--
Posted via http://www.ruby-....

26 Answers

Siep Korteling

2/6/2008 11:37:00 PM

0

Adam Akhtar wrote:
(...)
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)

I like the index/rindex trick. I have actualy done a similar job, going
through a big logfile to see which users were most active.
I opted for an array of hashes, with the frequency as value.
My result would have been:
[{1=>3},{2=>4},{3=>2},{4=>1}]
Can adapt your code to do the same?
--
Posted via http://www.ruby-....

Rob Biedenharn

2/6/2008 11:50:00 PM

0

On Feb 6, 2008, at 6:20 PM, Adam Akhtar wrote:

> As some of you may know from previous threads im trying to practice
> specific areas of ruby. One simple exercise i set myself was taking a
> elements in an array such as
>
> [1,2,3,1,2,2,4,3,2,1]
>
> and then grouping them together and putting them in subarrays within
> an
> array like this
>
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> Heres how i did it (im new to both programming and ruby)
>
> def subpack(list)
> uniqlist = Array.new
> subpacklist = Array.new
> list.sort!
>
> uniqlist = list.uniq
> uniqlist.each_index do |i|
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)
> end
> subpacklist
> end
>
>
> WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
> i.e. give me some hints, the names of some methods but not the
> solution
> as i want a go myself.


Think about how you might do it with a pencil...
- write the values out in order "[ 1, 1, 1, 2, 2, 2, 2, 3, 3, 4 ]"
- if the "next" value is different from the last value processed (and
initially there is no last value), then start a new sub-array with
this single value (my pencil would insert a '[' between the '[' and '1')
- ...and if you'd already written a '[', you have to then write "],["
- if the next value is the same, move your pencil (since it goes in
the current sub-array)
- when you run out of values, close the final subarray with ']'

It shouldn't take much to think about how this turns into Ruby
(keeping in mind that Arrays are always finite so you don't really
have to "write" the ']')

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com

Adam Akhtar

2/6/2008 11:51:00 PM

0

Well you know what, as i was thinking of a solution to this, hashes
popped into my head. I didnt know how they would be used but soemthing
whired and said you could probably do it with them. I always tend to shy
away from them but seeing as you laid down the challenge ill go away and
try and replicate your way. cheers!

In the meantime keep em coming!



--
Posted via http://www.ruby-....

Adam Akhtar

2/6/2008 11:53:00 PM

0

ahh cheers for that one as well rob...im on the case.

--
Posted via http://www.ruby-....

Siep Korteling

2/6/2008 11:57:00 PM

0

Siep Korteling wrote:
(...)
> [{1=>3},{2=>4},{3=>2},{4=>1}]
> Can adapt your code to do the same?

Sorry. This should read
{1=>3, 2=>4, 3=>2, 4=>1}
Can you adapt your code to do the same?

Maybe you'd have to read up on hashes before you planned to. I'm
learning the same way as you do. Getting some grip on Fixnum, String,
Array, Hash, File and Date enabled me to write better scripts then I did
with any other scripting language.

Regards,

Siep

--
Posted via http://www.ruby-....

7stud --

2/7/2008 12:04:00 AM

0

Adam Akhtar wrote:
> As some of you may know from previous threads im trying to practice
> specific areas of ruby. One simple exercise i set myself was taking a
> elements in an array such as
>
> [1,2,3,1,2,2,4,3,2,1]
>
> and then grouping them together and putting them in subarrays within an
> array like this
>
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> Heres how i did it (im new to both programming and ruby)
>
> def subpack(list)
> uniqlist = Array.new
> subpacklist = Array.new
> list.sort!
>
> uniqlist = list.uniq
> uniqlist.each_index do |i|
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)
> end
> subpacklist
> end
>
>
> WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
> i.e. give me some hints, the names of some methods but not the solution
> as i want a go myself.

Another way would be to iterate over your original array and add each
number to a Hash as you go. A Hash keys will be one of the numbers,
and the corresponding value will be an array containing all of that
particular number.

You can create a new Hash so that when you access a non existent key in
a Hash an empty array will be returned. See the block form of
Hash.new().

Finally, you can create your final array from the hash values using
Hash#values.

--
Posted via http://www.ruby-....

Todd Benson

2/7/2008 12:22:00 AM

0

On Feb 6, 2008 5:20 PM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> As some of you may know from previous threads im trying to practice
> specific areas of ruby. One simple exercise i set myself was taking a
> elements in an array such as
>
> [1,2,3,1,2,2,4,3,2,1]
>
> and then grouping them together and putting them in subarrays within an
> array like this
>
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> Heres how i did it (im new to both programming and ruby)
>
> def subpack(list)
> uniqlist = Array.new
> subpacklist = Array.new
> list.sort!
>
> uniqlist = list.uniq
> uniqlist.each_index do |i|
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)
> end
> subpacklist
> end
>
>
> WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
> i.e. give me some hints, the names of some methods but not the solution
> as i want a go myself.

My solution uses #uniq, #each, #<<, #select, and #==. Oh, and #[]

Todd

Curt Sampson

2/7/2008 12:55:00 AM

0

On 2008-02-07 08:20 +0900 (Thu), Adam Akhtar wrote:

> [1,2,3,1,2,2,4,3,2,1]
> and then grouping them together and putting them in subarrays within an
> array like this
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]

The Array#assoc method is your friend here.

If you have questions or comments on this, please Cc: me directly as
well as sending to the list. (This should happen automatically if the
list doesn't wipe out my reply-to header.)

cjs
--
Curt Sampson <cjs@starling-software.com> +81 90 7737 2974
Mobile sites and software consulting: http://www.starling-so...

Clifford Heath

2/7/2008 1:26:00 AM

0

Curt Sampson wrote:
> On 2008-02-07 08:20 +0900 (Thu), Adam Akhtar wrote:
>> [1,2,3,1,2,2,4,3,2,1]
>> and then grouping them together and putting them in subarrays within an
>> array like this
>> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> The Array#assoc method is your friend here.

Really? Where's the array containing arrays come from?

------------------------------------------------------------ Array#assoc
array.assoc(obj) -> an_array or nil
------------------------------------------------------------------------
Searches through an array whose elements are also arrays comparing
obj with the first element of each contained array using obj.==.
Returns the first contained array that matches (that is, the first
associated array), or nil if no match is found. See also
Array#rassoc.

Clifford Heath.

Christopher Dicely

2/7/2008 2:15:00 AM

0

Anytime I want to "build up" something from an existing Enumerable
object (Array, etc.) in Ruby, I usually look to inject or collect; the
most elegant of the way I've come up with of doing exactly what you
want uses ==, *, uniq, collect, find_all, and size, and the function
body fits on a single line without semi-colons. (I'm not holding up
that it is a one-liner as a strength, just as a description.)

On Feb 6, 2008 3:20 PM, Adam Akhtar <adamtemporary@gmail.com> wrote:
> As some of you may know from previous threads im trying to practice
> specific areas of ruby. One simple exercise i set myself was taking a
> elements in an array such as
>
> [1,2,3,1,2,2,4,3,2,1]
>
> and then grouping them together and putting them in subarrays within an
> array like this
>
> [ [1,1,1], [2,2,2,2] , [3,3] ,[4] ]
>
> Heres how i did it (im new to both programming and ruby)
>
> def subpack(list)
> uniqlist = Array.new
> subpacklist = Array.new
> list.sort!
>
> uniqlist = list.uniq
> uniqlist.each_index do |i|
> rangeleft = list.index(uniqlist[i])
> rangeright = list.rindex(uniqlist[i])
> subpacklist << list.slice(rangeleft..rangeright)
> end
> subpacklist
> end
>
>
> WITHOUT GIVING ME YOUR SOLUTION IN FULL, how could i improve this...
> i.e. give me some hints, the names of some methods but not the solution
> as i want a go myself.
> --
> Posted via http://www.ruby-....
>
>