Here's a hint. Rather than looping through the array as is and trying to pick out the min and max, why not sort the array into ascending order and print the first and last members of it?
<!--
Sincerely,
Thomas Murphy
646.957.6115
-->
···
On Nov 29, 2013, at 7: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:
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
···
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:
(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]
Oops, you were printing the indices of the min and max values, and as your array has the property that array = x + 1 it wasn't entirely clear in my mind whether the # refers to the value in the array or the index. Again if efficiency is no issue:
In theory, yes. In practice since many Array methods are implemented
in C it may actually be faster to sort and then retrieve Array#first
and Array#last. Measuring is the only way to find out.
But, yes, I prefer to avoid the sort and iterate once because it is
the least invasive thing to do (from a theoretical perspective). You
just need one traversal to know min and max.
Kind regards
robert
···
On Sat, Nov 30, 2013 at 4:05 AM, Matthew Kerwin <matthew@kerwin.net.au> wrote:
Point of order: it's always as fast or faster to iterate once than to sort.
"I actually had to NOT use the Min and Max methods. Rather, I have to
simulate them myself. It's for school." - Adam Tonto in previous
e-mail in this thread.
···
On Mon, Dec 9, 2013 at 2:34 PM, Regis d'Aubarede <lists@ruby-forum.com> wrote:
Adam Tonto wrote in post #1129014:
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.
i am sure this has been asked before, but I am going to ask again, sorry!
Does someone have a code fragment showing how to read a CSV file, add one
or more columns to the front or back, and then write it back out to a new
file?
No processing of the CSV other than to add columns, preferably being passed
in to the program as parameters to be inserted.
This is a completely different subject. Please start a new thread and
do not hijack threads.
Cheers
robert
···
On Sat, Nov 30, 2013 at 4:06 PM, John Apps <johndapps@gmail.com> wrote:
i am sure this has been asked before, but I am going to ask again, sorry!
Does someone have a code fragment showing how to read a CSV file, add one or
more columns to the front or back, and then write it back out to a new file?
No processing of the CSV other than to add columns, preferably being passed
in to the program as parameters to be inserted.