ruby max.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby max.rb相关的知识,希望对你有一定的参考价值。

# # Find the maximum 
# def maximum(arr)
#   arr.max
# end

# # expect it to return 42 below
# result = maximum([2, 42, 22, 02])
# puts "max of 2, 42, 22, 02 is: #{result}"

# # expect it to return nil when empty array is passed in
# result = maximum([])
# puts "max on empty set is: #{result.inspect}"

# result = maximum([-23, 0, -3])
# puts "max of -23, 0, -3 is: #{result}"

# result = maximum([6])
# puts "max of just 6 is: #{result}"

##########################################################

def maximum(array)
  i             = 0
  max_number    = array[i]
  index_of_max  = i

  while (i < (array.length))
    if (max_number < array[i])
      max_number    = array[i]
      index_of_max  = i
    end
    i = i + 1
  end

  print "The highest number in the array, #{array}, is #{max_number}; which occurs at index position #{index_of_max}."
  print "\n"
end

# Largest number at the end:
maximum([2, 42, 22, 02, 66])

# Largest number at the beginning:
maximum([222, 42, 22, 02, 66])

# Largest number somewhere in the middle:
maximum([222, 4200, 22, 02, 66])

以上是关于ruby max.rb的主要内容,如果未能解决你的问题,请参考以下文章

ruby max_value_importer.rb

ruby [Ruby Cheat] Cheatsheet #ruby

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby