开发者

Max and Min Value ... Need method to return two variable values

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 re开发者_运维技巧turn=%d\n", maxmin(list)) 


Using 1.9.1, there's minmax

>> list=[4,6,10,7,1,2]
=> [4, 6, 10, 7, 1, 2]
>> list.minmax
=> [1, 10]


Max and Min method are already in Stdlib or ruby

  • Enumerable#max
  • Enumerable#min

So use it

list.max
list.min


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


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


The problem is that you try to use global variables (that are called like that : @max, @min) but you want your code to assign the values, that you don't even assign. You'd rather choose local variables over global, if it's possible, because of accessibility.

The second problem is that in the case you use global variable, you don't have to return anything. for exemple :

@value = 0

def test
  @value = 1
end

puts @value ==> 0
test # change @value to 1
# it also return 1 because ruby return last statement value
puts @value ==> 1

In the case you use local variables you should return multiple results. That's where Ruby do the job, Ruby automatically cast multiple return statement to array and array to multiple variables assignment)

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

def maxmin(list)

  f = list.shift
  if list.empty? then
      return f, f # f is the minimum and the maximum of a list of one element
  end

  mi, ma = maxmin(list)
  if (f > ma) then
    ma = f
  elsif (f < mi)
    min = f
  end

return mi, ma
end

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

Your way of doing is pretty fun (love recursivity) but it's not really elegant, the performances are not really good and moreover it's a bit confusing, that's far from ruby vision.

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

def minmax(list)
  max = list[0]
  min = list[0]

  list.each do |elem|
    if elem > max then
      max = elem
    elsif elem < min
      min = elem
    end
  end

  return min, max
end

min, max = minmax(list)
printf("max=#{max}, min=#{min}") 

Is a clearer version of your code, even if less cool. You can try those answers with global variables it should be easy.

Obviously because of Ruby vision when you're done with that you're welcome to use Array.max and Array.min.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜