On Nov 30, 2013 12:32 PM, "Abinoam Jr." <abinoam / gmail.com> wrote:
>
> On Fri, Nov 29, 2013 at 9:25 PM, Adam Tonto <lists / ruby-forum.com> wrote:
>
> > I'm not looking for anyone to GIVE ME the answer/ do homework for me.
> > I'm just looking for a turn in the right direction. Here is my code:
>
> :-D :-)  (attack is the best form of defense)
>
> So, the direction...
>
> When iterating through the elements of an Array you can use
> "iterators" rather than a while loop.[1]
>
> So if you really need to iterate you should...
>
> array.each do |element|
>   # use the "element"
> end
>
> And "each" will take care of the array.lenght limit.
>
> Another generic algorithm hint... you don't need to iterate two times
> (one for max and one for min).
> You can go on checking for max AND min at the same run, this saves you
> the half of the time.
>
> But, as Thomas has pointed out, you don't need to iterate.
> You can use the instance methods of the Array class to "sort" the
> Array and they will probably to the trick in a faster way.[2]

Point of order: it's always as fast or faster to iterate once than to sort.

If using instance methods is allowed, doesn't Array have #max and #min
methods?

I'd say: if you want to do something to every element (e.g. compare it to
something) use #each, and if you also want to know their positions use
#each_with_index

Sent from my phone, so excuse the typos.