Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / shootout / knucleotide.ruby-2.ruby
blob6cc009b689f2d20ece297d676055975cd5504aa6
1 # The Computer Language Shootout
2 # http://shootout.alioth.debian.org
4 # contributed by jose fco. gonzalez
5 # modified by Sokolov Yura
7 seq = String.new
9 def frecuency( seq,length )
10 n, table = seq.length - length + 1, Hash.new(0)
11 f, i = nil, nil
12 (0 ... length).each do |f|
13 (f ... n).step(length) do |i|
14 table[seq[i,length]] += 1
15 end
16 end
17 [n,table]
19 end
21 def sort_by_freq( seq,length )
22 n,table = frecuency( seq,length )
23 a, b, v = nil, nil, nil
24 table.sort{|a,b| b[1] <=> a[1]}.each do |v|
25 puts "%s %.3f" % [v[0].upcase,((v[1]*100).to_f/n)]
26 end
27 puts
28 end
30 def find_seq( seq,s )
31 n,table = frecuency( seq,s.length )
32 puts "#{table[s].to_s}\t#{s.upcase}"
33 end
35 line = STDIN.gets while line !~ /^>THREE/
36 line = STDIN.gets
37 while (line !~ /^>/) & line do
38 seq << line.chomp
39 line = STDIN.gets
40 end
42 [1,2].each {|i| sort_by_freq( seq,i ) }
44 %w(ggt ggta ggtatt ggtattttaatt ggtattttaatttatagt).each{|s| find_seq( seq,s) }