Max and Min Value ... Requires a method to return two variable values

I am new to coding and I need help understanding what is wrong with my logic and syntax in the following method ... The program should return the max and min values ​​of the array. My goal was to have two variables (max and min) outside the method, so that as the method passes through the array, the values ​​will be 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)) 
+3
source share
5 answers

Using 1.9.1, minmax

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

The Max and Min method is already in Stdlib or ruby

Therefore use it

list.max
list.min
+3
source

: ? , , :

return min_value, max_value

, (, ), , . , . , , , , , , , .

( Ruby, , , , ), :

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
+3

. , .

, , .

-, , , , .

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

, , , , return.

-, , 3 4, minmax, .

, - , , , , :

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

, ( : @max, @min), , , . , , - .

, . :

@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

. , Ruby , Ruby )

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 it is quite funny (love recursion), but it’s not very elegant, the performances are not really good, and besides, it is a bit confusing, which is far from the 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}") 

It is a clearer version of your code, even if it is less cool. You can try these answers with global variables, this should be easy.

Obviously, from the perspective of Ruby, when you are done with this, you can use Array.max and Array.min.

0
source

Source: https://habr.com/ru/post/1772244/


All Articles