Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

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)) 
like image 387
David Avatar asked Jul 31 '26 17:07

David


1 Answers

Using 1.9.1, there's minmax

>> list=[4,6,10,7,1,2]
=> [4, 6, 10, 7, 1, 2]
>> list.minmax
=> [1, 10]
like image 200
ghostdog74 Avatar answered Aug 02 '26 08:08

ghostdog74



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!