Trying to make MIN MAX sorting array; I want to print the min and max #s of the array

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:


array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
min = 0
max = 0
x = 0
i = array.length - 1

while x < array.length - 1
if array[x] > array[x - 1]
max = x
end
x+=1
end

while i > 0 array.length - 1
if array[i] < array[i + 1]
min = i
i-=1
end
end

puts “Min is: #{min}.”
puts “Max is: #{max}.”


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?

On Fri, Nov 29, 2013 at 9:25 PM, Adam T. [email protected]
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:

:smiley: :slight_smile: (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]

#1 -
http://www.public.traineronrails.com/courses/ruby/pages/012-rubyiterators.html
#2 - http://www.ruby-doc.org/core-2.0.0/Array.html

Show us your improved code (with the hints applied) !

Abinoam Jr.

On Nov 30, 2013 12:32 PM, “Abinoam Jr.” [email protected] wrote:

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

Sent from my phone, so excuse the typos.

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.

Old:
1,2,3,4

New:
a,b,1,2,3,4,c

​Thank you, John​

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.

Kind regards

robert

On Nov 29, 2013, at 7:25 PM, Adam T. [email protected] wrote:

i-=1
end
end

puts “Min is: #{min}.”
puts “Max is: #{max}.”



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

If efficiency isn’t an issue then you could take advantage of the
Enumerable module which an Array will already have included

ratdog:~ mike$ pry
[1] pry(main)> array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2] pry(main)> puts “Min is: #{array.min}”;
Min is: 1
[3] pry(main)> puts “Max is: #{array.max}”;
Max is: 10

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

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:

[1] pry(main)> array = (1 … 10).to_a.shuffle
=> [1, 9, 4, 8, 6, 10, 2, 3, 5, 7]
[2] pry(main)> max_val = array.max
=> 10
[3] pry(main)> min_val = array.min
=> 1
[4] pry(main)> max_index = array.find_index(max_val)
=> 5
[5] pry(main)> min_index = array.find_index(min_val)
=> 0

Hope this helps,

Mike

On Nov 30, 2013, at 11:00 AM, Mike S. [email protected] wrote:

max = 0
while i > 0 array.length - 1

[3] pry(main)> puts “Max is: #{array.max}”;

The “`Stok’ disclaimers” apply.

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

I actually had to NOT use the Min and Max methods. Rather, I have to
simulate them myself. It’s for school.

On Sat, Nov 30, 2013 at 4:06 PM, John A. [email protected] 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.

This is a completely different subject. Please start a new thread and
do not hijack threads.

Cheers

robert

I almost made it work. I don’t know how to call my method. Could I have
some help?

Here’s my code:


array = [3, 2, 3, 4, 5, 6, 7, 8, 9, 8]
min = 0
max = 0
x = 0
i = array.length - 1

def sortArray(a)
for x in array
if array[x] > array[x-1]
self[x], self[x-1] = self[x-1], self[x]
x += 1
end
end
end

array = sortArray(array)
min = array[0]
max = array[i]

puts “Min is: #{min}.”
puts “Max is: #{max}.”


On Tue, Dec 3, 2013 at 6:40 AM, Adam T. [email protected] wrote:

I actually had to NOT use the Min and Max methods. Rather, I have to
simulate them myself. It’s for school.

Well, then iterate the Array once and record min and max as you go.

Kind regards

robert

In Ruby there is a method called min and max that you can call on
arrays.

My approach would be.

list_of_nums = [3, 2, 3, 4, 5, 6, 7, 8, 9, 8]
@min = 0
@max = 0

def sortArray(array)
@min += array.min
@max += array.max
end

sortArray(list_of_nums)
@min => 2
@max => 9

Some tips:

  1. The for loop doesn’t work like you are expecting.
    It “yields” each element on “x”. It doesn’t yield the element index.
    Look:

array = [256, 3.14, “banana”]

for x in array
puts x
end

It will print
256
3.14
banana

Not:
0
1
2

So, if at each iteration the “x” will hold the value (not the index)
how do you expect this fragment to work?

if array[x] > array[x-1]

  1. What are the correct?

You are getting the argument in “a” with

def sortArray(a)

But it’s trying to sort “array” in

for x in array

… And trying to sort “self” in

Adam T. 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.

p [3,2,1].minmax

:wink:

Some tips:

  1. The for loop doesn’t work like you are expecting.
    It “yields” each element on “x”. It doesn’t yield the element index.
    Look:

array = [256, 3.14, “banana”]

for x in array
puts x
end

It will print
256
3.14
banana

Not:
0
1
2

So, if at each iteration the “x” will hold the value (not the index)
how do you expect this fragment to work?

if array[x] > array[x-1]

  1. What are the correct?

You are getting the argument in “a” with

def sortArray(a)

But it’s trying to sort “array” in

for x in array

… And trying to sort “self” in

self[x], self[x-1] = self[x-1], self[x]

Show us any progress you make.
I hope you learn in time to get a good mark.

Best regards,
Abinoam Jr.

“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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs