Is recusion the best?

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)

On Fri, Jul 24, 2009 at 11:47 AM, Li Chen[email protected] wrote:

Li

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

Harry K. wrote:

Harry

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

Li

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

Li

Well, it is much faster, but it will start to feel very slow, too if
your word size gets much bigger.

Harry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs