Emacs/putty code help

Discussion in 'Ruby' started by Thierry Ntoh yuh, Nov 5, 2010.

  1. 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.

    --
    Posted via http://www.ruby-forum.com/.
    Thierry Ntoh yuh, Nov 5, 2010
    #1
    1. Advertising

  2. Thierry Ntoh yuh

    w_a_x_man Guest

    On Nov 5, 12:12 am, Thierry Ntoh yuh <> wrote:
    > 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.
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    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]"
    w_a_x_man, Nov 5, 2010
    #2
    1. Advertising

  3. On Fri, Nov 5, 2010 at 7:12 AM, Thierry Ntoh yuh <> wr=
    ote:
    > 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.
    Jesús Gabriel y Galán, Nov 5, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ulysse
    Replies:
    8
    Views:
    1,033
    Ulysse
    Apr 2, 2007
  2. Pvt Ryan

    Help with putty code

    Pvt Ryan, Jul 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    478
    Pvt Ryan
    Jul 27, 2008
  3. Arihan Sinha
    Replies:
    1
    Views:
    177
    Jeremy Bopp
    Oct 28, 2010
  4. Thierry Ntoh yuh

    Emacs/putty code help [linux]

    Thierry Ntoh yuh, Nov 11, 2010, in forum: Ruby
    Replies:
    1
    Views:
    112
    Shadowfirebird
    Nov 11, 2010
  5. Adam Funk
    Replies:
    4
    Views:
    232
    Adam Funk
    Jan 29, 2007
Loading...

Share This Page