Emacs/putty code help

  • Thread starter Thierry Ntoh yuh
  • Start date
T

Thierry Ntoh yuh

Please someone help me with this ruby code on putty/emacs
Thanks

Write a statistical program in Ruby to:

a) Input the name of a data file from the command line arguments.

b) Input floating-point numbers from the data file.

c) Calculate and output the mean. This is the average, or the sum of
the
values divided by the number of values.

d) Calculate and output the median. This is the value such than half
the
values are larger and half are smaller. If you have an even number
of
values use either middle value or the average of the two middle
values.
 
W

w_a_x_man

Please someone help me with this ruby code on putty/emacs
Thanks

Write a statistical program in Ruby to:

a)  Input the name of a data file from the command line arguments.

b)  Input floating-point numbers from the data file.

c)  Calculate and output the mean.  This is the average, or the sum of
the
    values divided by the number of values.

d)  Calculate and output the median.  This is the value such than half
the
    values are larger and half are smaller.  If you have an even number
of
    values use either middle value or the average of the two middle
values.

I wonder what your teacher will think of this.

ruby -e"a=gets(nil).scan(/[-\d.]+/).map &:to_f;p a.
reduce{|s,x|s+x}/a.size,a.sort[a.size/2]"
 
J

Jesús Gabriel y Galán

Please someone help me with this ruby code on putty/emacs
Thanks

Write a statistical program in Ruby to:

a) =A0Input the name of a data file from the command line arguments.

Take a look at ARGV
b) =A0Input floating-point numbers from the data file.

File#readlines and Array#map and String#to_f or Kernel#Float to
transform to floats
c) =A0Calculate and output the mean. =A0This is the average, or the sum o= f
the
=A0 =A0values divided by the number of values.

sum =3D array.inject {|total,n| total + n} # this gives you the sum of the =
values
d) =A0Calculate and output the median. =A0This is the value such than hal= f
the
=A0 =A0values are larger and half are smaller. =A0If you have an even num= ber
of
=A0 =A0values use either middle value or the average of the two middle
values.

array.sort[array.size / 2] # will sort the array and take the middle
value (or the left side one if even)

Hope this sets you on the right track. Let us know how it goes.

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top