Problem: http://rosalind.info/problems/dna/
Solved by Perl
use strict;
use warnings;
while (my $dna = <>) {
chomp $dna;
my @symbols = split //, $dna;
my %count = (
'A' => 0,
'C' => 0,
'G' => 0,
'T' => 0,
);
for my $symbol (@symbols) {
$count{$symbol}++;
}
print $count{'A'} . ' ' ,
$count{'C'} . ' ' ,
$count{'G'} . ' ' ,
$count{'T'} . "\n";
}
實際寫頗土炮。Solved by Python
import sys
dna = sys.stdin.read().strip()
print(' '.join([str(dna.count(i)) for i in 'ACGT']))
不得不說演化過的 Python 習得各家語言的優點,才能淬鍊精幹短小的程式碼。沒有分號倒是很有意思,強迫 programmers 一行不能寫兩個以上的 statements。
Solved by Ruby:
dna = STDIN.read.chomp
puts [dna.count('A'), dna.count('C'), dna.count('G'), dna.count('T')].join ' '
短短的蠻可愛的
python 在collection有Counter這個函式~~這題很適合它~~
回覆刪除