how to get Mycompute.sum([1,2,3,4]).mean=2.5?

P

Pen Ttt

class Mycompute
def self.sum(a)
a.inject(0){|s,i|s+i}
end
def self.mean(a)
a.inject(0){|s,i|s+i}/(a.size.to_f)
end
end
Mycompute.sum([1,2,3,4])
=> 10
Mycompute.mean([1,2,3,4])
=> 2.5

how can i get Mycompute.sum([1,2,3,4]).mean=2.5?
 
B

botp

class =A0Mycompute
=A0def =A0self.sum(a)
=A0 =A0a.inject(0){|s,i|s+i}
=A0end
=A0def =A0self.mean(a)
=A0 =A0a.inject(0){|s,i|s+i}/(a.size.to_f)
=A0end
end
Mycompute.sum([1,2,3,4])
=3D> 10
Mycompute.mean([1,2,3,4])
=3D> 2.5

how =A0can i =A0get =A0Mycompute.sum([1,2,3,4]).mean=3D2.5?

uh,uh, you cant because you get a conflict: your sum returns a value
(not array), and your mean is chaining on sum's return value, but mean
in turn requires its values coming fr its arguments.... in short:
confusing...

my suggestion is, start simple as possible, eg

$ irb?> x =3D Mycompute.new([1,2,3,4])
10.0
=3D> 10.02.5
=3D> 2.5[1, 2, 3, 4]
=3D> [1, 2, 3, 4]1
=3D> 14
=3D> 4[1, 4, 9, 16]
=3D> [1, 4, 9, 16]

kind regards -botp
 
P

Pen Ttt

here is my code:
def mean(n)
s=0
m=0
self.each{|item|
if item>n then
puts item
s=s+n
m=m+1
else
end}
return s/(m.to_f)
end
here is output
[1,2,3,4].mean(2)
=> 2.0
why i can't get
[1,2,3,4].mean(2)
=> 3.5
 
P

Pen Ttt

sorry,i correct it
def mean(n)
s=0
m=0
self.each{|item|
if item>n then
puts item
s=s+item
m=m+1
else
end}
return s/(m.to_f)
end
 
A

Adam Prescott

data = [1, 2, 3, 4]
sum = data.inject { |a, b| a + b } #=> 10
sum/data.size.to_f #=> 2.5
 
P

Pen Ttt

require 'rubygems'
require 'mysql'
class Analyse
def self.get(from,to)
open('/tmp/result','w') do |wfile|
dbh = Mysql.real_connect("localhost", "root", "******")
dbh.query("use stock;")
result=dbh.query("select symbol,date,open,high,low,close \
from `#{symbol}` where (date > from and date< to) order by date
asc;")
compute=[]
result.each{|row| wfile.puts row.join(";")}
end
end

def self.mean
amean=[]
open('/tmp/result','r'){|rfile|
line=rfile.readlines
line.each{|item|
amean<< item.chomp.split(";")[-1].to_f
}}
amean=amean.compact
amean.inject(0){|s,n| s+n}/(amean.size.to_f)
end
end
end
input
Analyse.get("2010-01-1","2010-09-1") can get a file /tmp/result
again input
Analyse.mean can get what i need,
but Analyse.get("2010-01-1","2010-09-1").mean can't work,
would you mind to tell me how to fix the mean method to make

Analyse.get("2010-01-1","2010-09-1").mean can work??
 
P

Pen Ttt

a friend help me solve it,

require 'rubygems'
require 'mysql'
class Analyse
def self.get(from,to)
object = Analyse.new
open('/tmp/result','w') do |wfile|
dbh = Mysql.real_connect("localhost", "root", "******")
dbh.query("use stock;")
result=dbh.query("select symbol,date,open,high,low,close \
from `#{symbol}` where (date > from and date< to) order by date
asc;")
compute=[]
result.each{|row| wfile.puts row.join(";")}
end
return object
end

def self.mean
amean=[]
open('/tmp/result','r'){|rfile|
line=rfile.readlines
line.each{|item|
amean<< item.chomp.split(";")[-1].to_f
}}
amean=amean.compact
amean.inject(0){|s,n| s+n}/(amean.size.to_f)
end
end
end
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top