计算数字平均值

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# Gets the Average of a list of numbers.
def Average(array)
  # Placeholder for our answer
  answer = 0
  # Placeholder for our sum
  sum = 0
  # Get the length of the items in the array
  length = array.length
  # Loop through each number in our array
  array.each do |number|
    # Add each number to our current sum
    sum = sum + number.to_i
    # Exit the loop
  end
  # Get the average
  answer = (sum / length)
  # Return the answer
  return (sum / length)
  # Exit the method
end

# Gets the Average of a list of numbers.
def Average2(string)
  # Placeholder for our answer
  answer = 0
  # Placeholder for our sum
  sum = 0
  # Create an array from our list of numbers
  array = string.split(',')
  # Get the length of the items in the array
  length = array.length
  # Loop through each number in our array
  array.each do |number|
    # Add each number to our current sum
    sum = sum + number.to_i
    # Exit the loop
  end
  # Get the average
  answer = (sum / length)
  # Return the answer
  return (sum / length)
  # Exit the method
end