1

input: A list of numbers from the keyboard. output: The median of the input numbers

I need the whole code

def median(array)
array.sort!
  if (array.length % 2==1 )
     return array[array.length/2.0]
  else
     return (array[array.length/2] + array[(array.length/2)-1])/2.0
  end
end

How can I enter list from keyboard and the find the median?

user3261528
  • 21
  • 1
  • 3
  • 1
    Is there anything wrong with your `median` method? If not, your question should be simply: “How to get array of numbers from keyboard“. – Andrew Marshall Feb 02 '14 at 01:10

1 Answers1

2

Assuming you want numbers separated by spaced on a single input line (i.e. 1 5 56 6 75), add the following to your script:

input_array = gets.chomp.split(" ")

Then pass input_array to your median method

Update: Note that input_array will be an array of strings, so you'll need to convert values to integers. Here's a good example on doing so.

Community
  • 1
  • 1
Nathaniel Mallet
  • 401
  • 2
  • 10