tags:

views:

75

answers:

5

Hello Everyone, I am new to coding and need help understanding what is wrong with my logic and or syntax in the following method... The program is supposed to return the max and min values of an array. My goal was to have two variables (max and min) outside of the method, so that as the method ran through the array the values would get replaced accordingly. thank you for your help...

    list=[4,6,10,7,1,2]

max=list[0]
min=list[0]

def maxmin(list)

  f=list.shift
  if list.empty?then
      return max = f 
      return min = f
  end

  t=maxmin(list)
  if(f>t) then
    return max = f 
    return min = t
  else
    return max = t 
    return min = f
  end

end

printf("max=#{max}, min=#{min}, method return=%d\n", maxmin(list)) 
A: 

Try

irb(main):001:0> a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb(main):002:0> a.max
=> 5
irb(main):003:0> a.min
=> 1
irb(main):004:0>

These two methods are built into the Array class.

Finglas
A: 

Max and Min method are already in Stdlib or ruby

So use it

list.max
list.min
shingara
+1  A: 

Using 1.9.1, there's minmax

>> list=[4,6,10,7,1,2]
=> [4, 6, 10, 7, 1, 2]
>> list.minmax
=> [1, 10]
ghostdog74
+1  A: 

Edit: is your question just about returning two variable? If so, just separate them with a comma, and they will be returned as an array:

return min_value, max_value

To add to what has already been written (yes, use the built-in libraries), it is generally a bad idea to modify variables outside of the method they are being used in. Note that in framework call, new values are being returned. This lets the person calling the method decide what to do with those values, rather than this method assuming that the variables exist and then changing them all the time.

If I had to write it (and I'm new to Ruby so I might not be doing this as elegantly as possible, but it should be easy to follow), I would write it something like this:

def find_min_max(list)
  if (list.nil? || list.count == 0)
    return nil, nil
  end

  min = list.first
  max = list.first

  list.each do |item|
    if item.nil?
      next
    elsif item < min
      min = item
    elsif item > max
      max = item
    end
  end

  return min, max
end

list = [1, 439, 2903, 23]
min_max = find_min_max list

p min_max
Travis
A: 

I would agree with the other answers given. There is no point writing this method, other than as a programming exercise.

There are a few problems with the logic which might explain why you are not getting the result you expect.

Firstly there are 3 pairs of return statements, the second of which would never be called because the method has already returned, e.g.

return max = f
return min = f # never gets called

You need to return both the min and max values to make the recursive algorithm work so I guess you need to return a pair of values or an array in a single return statement.

Secondly, your min and max variables initialized on lines 3 and 4 are not in scope within the minmax method body so you are actually defining new local variables there.

If you adapt your code you might end up with something like this, but this is not a method you need to write in production and I'm sure there are better ways to do it:

list = [4,6,10,7,1,2]

def maxmin(list)
  f = list.shift
  if list.empty?
    return f, f
  end

  max, min = maxmin(list)
  return f > max ? f : max, f < min ? f : min
end

max, min = maxmin(list)
puts "min = #{min}, max = #{max}"
Steve