Is recusion the best?

L

Li Chen

Hi all,

I have some codes implemented with recursion. When the loop/recursion
number is small everything looks fine to me. But the program starts
slow when the loop number is >7.I wonder if recursion is the best when
the recursion number is >7?


Thanks,

Li

#########################def table(array,word_size=1,word='')

hash_table=Hash.new(0)

# create the keys first
array.each do |e|
if word_size==1
hash_table["#{word+e}" ]=0

else
hash_temp=table(array,word_size-1,word+e )
hash_table.merge!(hash_temp)

end
end

# create the values based on keys
hash_table.each do |k,v|
hash_table[k]=k.tr('ACGT','0123').to_i(4)
end

return hash_table
end

######################
array=['A','C','G','T']
#array=['A']
p table(array,10)
 
H

Harry Kakueki

Hi all,

I have some codes =A0implemented with recursion. When the loop/recursion
number is small =A0everything looks fine to me. But the program starts
slow when the loop number is >7.I wonder if recursion is the best when
the recursion number is >7?


Thanks,

Li

Does this do the same thing?

hash_table=3DHash.new(0)
wordsize =3D 10

(0...4**wordsize).each do |f|
hash_table[f.to_s(4).rjust(wordsize,"0").tr('0123','ACGT')] =3D f
end

p hash_table


Harry


--=20
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby/list.html
 
L

Li Chen

Harry said:
Does this do the same thing?

hash_table=Hash.new(0)
wordsize = 10

(0...4**wordsize).each do |f|
hash_table[f.to_s(4).rjust(wordsize,"0").tr('0123','ACGT')] = f
end

p hash_table


Harry

Yes, they are the same. GOOD JOB!!!

Li
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top