The Computer Language Shootout

Vlad Dumitrescu vlad_dumitrescu@REDACTED
Fri Mar 17 11:48:59 CET 2006


Hi,

Another improvement can be obtained with 

dna_seq( Seq ) ->
    case io:get_line('') of
        eof  -> lists:flatten(lists:reverse(Seq));
        Line -> Uline = to_upper_no_nl(Line),
		dna_seq([Uline|Seq])
    end.


Also, an even better result (~half the time) can be obtained by using
binaries instead of lists:


dna_seq( Seq ) ->
    case io:get_line('') of
        eof  -> list_to_binary(lists:reverse(Seq));
        Line -> Uline = to_upper_no_nl(Line),
		dna_seq([Uline|Seq])
    end.

gen_freq(Dna, Len) ->
    gen_freq(Dna, Len, dict:new(),0,size(Dna)).

gen_freq(<<>>, _, Frequency, Acc, _) ->
    {Frequency,Acc};

gen_freq(Dna, Len, Frequency, Acc, Dec) when Dec >= Len ->
    <<Key:Len/binary, _/binary>> = Dna, 
    Freq = dict:update_counter(Key, 1, Frequency),
    <<_, T/binary>> = Dna,
    gen_freq(T, Len, Freq, Acc +1, Dec -1);

gen_freq(_, _, Frequency, Acc, _) ->
     {Frequency,Acc}.


printf([],_) -> 
    io:fwrite("\n");
printf([H |T],Tot)->
    {Nucleoid,Cnt}=H,
    io:fwrite("~s ~.3f\n",[binary_to_list(Nucleoid),(Cnt*100.0)/Tot]),
    printf(T,Tot).

write_count(Dna, Pattern) ->
    { Freq ,_} = gen_freq(Dna, size(Pattern)),
    case dict:find(Pattern,Freq) of
	{ok,Value} -> io:fwrite("~w\t~s\n",[Value,binary_to_list(Pattern)]);
	error      -> io:fwrite("~w\t~s\n",[0,binary_to_list(Pattern)])
    end.

main() ->
    Seq = dna_seq(),
    printf(gen_freq(Seq,1)),
    printf(gen_freq(Seq,2)),
    write_count(Seq,<<"GGT">>),
    write_count(Seq,<<"GGTA">>),
    write_count(Seq,<<"GGTATT">>),
    write_count(Seq,<<"GGTATTTTAATT">>),
    write_count(Seq,<<"GGTATTTTAATTTATAGT">>),
    ok.

Best regards,
Vlad



More information about the erlang-questions mailing list