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}."

________________________________________________________________________

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

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:

________________________________________________________________________

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 > 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}."

________________________________________________________________________

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

: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 - Class: Array (Ruby 2.0.0)

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

Abinoam Jr.

···

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:

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

···

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:

________________________________________________________________________

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 > 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}."

________________________________________________________________________

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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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:

: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]

Sent from my phone, so excuse the typos.

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

···

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

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}."

----------------------------------------------------------------------

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

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.

p [3,2,1].minmax

:wink:

···

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

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:

[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 Stok <mike@stok.ca> wrote:

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:

________________________________________________________________________

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 > 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}."

________________________________________________________________________

--
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 Stok <mike@stok.ca>
Mike Stok

The "`Stok' disclaimers" apply.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

Kind regards

robert

···

On Tue, Dec 3, 2013 at 6:40 AM, Adam Tonto <lists@ruby-forum.com> wrote:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

On Sat, Dec 7, 2013 at 10:12 PM, Adam Tonto <lists@ruby-forum.com> wrote:

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 > array[x-1]
      self, self[x-1] = self[x-1], self
      x += 1
      end
    end
  end

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

puts "Min is: #{min}."
puts "Max is: #{max}."

----------------------------------------------------------------------

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

--
John Moon

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 > array[x-1]

2) 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

···

On Sun, Dec 8, 2013 at 12:12 AM, Adam Tonto <lists@ruby-forum.com> wrote:

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 > array[x-1]
      self, self[x-1] = self[x-1], self
      x += 1
      end
    end
  end

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

puts "Min is: #{min}."
puts "Max is: #{max}."

----------------------------------------------------------------------

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

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

p [3,2,1].minmax

:wink:

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

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​

···

--
John Apps
+1 719 534 3910

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 > array[x-1]

2) 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, self[x-1] = self[x-1], self

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

Best regards,
Abinoam Jr.

···

On Mon, Dec 9, 2013 at 1:09 PM, Abinoam Jr. <abinoam@gmail.com> wrote:

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 > array[x-1]

2) 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

On Sun, Dec 8, 2013 at 12:12 AM, Adam Tonto <lists@ruby-forum.com> wrote:

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 > array[x-1]
      self, self[x-1] = self[x-1], self
      x += 1
      end
    end
  end

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

puts "Min is: #{min}."
puts "Max is: #{max}."

----------------------------------------------------------------------

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