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?
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]
When iterating through the elements of an Array you can use
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
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.
On Sat, Nov 30, 2013 at 4:05 AM, Matthew K. [email protected]
wrote:
Point of order: it’s always as fast or faster to iterate once than to sort.
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.
Oops, you were printing the indices of the min and max values, and as
your array has the property that array[x] = 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:
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.
“I actually had to NOT use the Min and Max methods. Rather, I have to
simulate them myself. It’s for school.” - Adam T. in previous
e-mail in this thread.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.