Array Number of Leaves

S

salai

Dear Rubyist,

I want to count array list [1,2,3,[4],5] ---> # 5 leaves
but I always get sum_of_array.

Where did I do wrong?


def array_leave1 # sum_of_array?
res = 0
self.each{|elem|
if elem.kind_of?(Array)
res += elem.sum_of_array
else
res += elem
end
}
return res
end

def number_of_leaves
self.inject(0){|acc,x|
if x.kind_of?(Array)
acc += x.number_of_leaves
else
acc += x
end
}
end

many thanks in advance.

regards,
salai
 
R

Robert Klemme

2009/10/26 salai said:
Dear Rubyist,

I want to count array list [1,2,3,[4],5] ---> # 5 leaves
but I always get sum_of_array.

Where did I do wrong?


=A0 =A0 =A0 =A0def array_leave1 # sum_of_array?

The method should have the same name as the one you call below
("elem.sum_of_array").
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0res =3D 0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.each{|elem|
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if elem.kind_of?(Array)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0res +=3D e= lem.sum_of_array
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0res +=3D e= lem
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0}
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return res
=A0 =A0 =A0 =A0end

def number_of_leaves
=A0 =A0self.inject(0){|acc,x|
=A0 =A0 =A0 =A0if x.kind_of?(Array)
=A0 =A0 =A0 =A0 =A0acc +=3D x.number_of_leaves
=A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0acc +=3D x

The line above should read acc +=3D 1. You could even simplify that as

acc +=3D (x.number_of_leaves rescue 1)
=A0 =A0 =A0 =A0end
=A0 =A0 =A0}
=A0end

Not sure what your issue is exactly. Please show the complete code
along with expected results. The code you presented cannot be the
real code because at least the class declaration is missing.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
B

Bertram Scharpf

Hi,

Am Montag, 26. Okt 2009, 21:17:17 +0900 schrieb Robert Klemme:
=20
The line above should read acc +=3D 1. You could even simplify that as
=20
acc +=3D (x.number_of_leaves rescue 1)
=20

I prefer doing it with `case'. I admit I did no performance check.

inject( 0) { |acc,x|
acc +=3D case x
when Array then x.number_of_leaves
else 1
end
}

Bertram

--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
 
R

Robert Klemme

2009/10/26 Bertram Scharpf said:
Hi,

Am Montag, 26. Okt 2009, 21:17:17 +0900 schrieb Robert Klemme:

I prefer doing it with `case'. I admit I did no performance check.

=A0inject( 0) { |acc,x|
=A0 =A0acc +=3D case x
=A0 =A0 =A0when Array then =A0 x.number_of_leaves
=A0 =A0 =A0else =A0 =A0 =A0 =A0 =A0 =A0 =A01
=A0 =A0end
=A0}

You're right, the solution with "rescue" is rather hackish. :)

My serious solution would be

acc +=3D Array =3D=3D=3D x ? x.number_of_leaves : 1

I prefer to use case for cases (sic!) which have more than two alternatives=
 

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
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top