给定一个字符串,如:
ATGGTCCCCTCTTGCCGCGGAAATAATTCCGATATGACCATGGGTAATACTCAAATAATGTAGTTGTGGGAGAGGTATCCACCGTCGGTAGATACTCCTCCGAGCGCTGGTTGGATGAGAGGTTTGTGTGCTTATATTACCGTGAAGCACAGGATCCAAGCCCCAGAGTCAGACCGTCATGTTTGCTTCCGCTGACCGATTACAGCGCTGGAACGTTATAAAGCGCCCACATATTAAGGCACATGACGCTCTCGTAGTTATTTGGGCCGTAATAAATCCAGGGTCTATTTAGCTCGCGCGAGTTTGCAGTGGGCCGACACTAGCAGTTTTGTTCGTAGAGACCTGGCCGAATATTGGCCTGACGAGAAAAGAAGGTGACCACACAATGTAACAGTTCCATATACACCGCACAAAGGGTCATATTATTACCGCCACAACTAGTCCTATCATCTCTGCTTTATCGAATCCAGGGGCAAGAAAAAGTACTGTAGAGTTACCCCGGGTCGGATATACAATGCCGGAAGTGCGTATCGCTACACTCAAGGCCACCCGATACGTCTCCAGCAAGCGGTGGTTGGGGCTGCCTTCAGATGTGTACGTTTCGTGGCAAAGCCTGCTTATATGGTGTTTAATCCAATCGTAGAGAAGGGCGAACCACGATACTGAGCCGACTCGATACGTTGCGGCGAGGCCGTAGCTCCTTTGGGAGTAAGTACAATCGTACACGTGTTAGGCTCTCCCAATATGTCGTAAATCAAACGAAGTATCCAATGGCCTTCCATAAGCCCACCGTCGTCGCATATTAAGGTAGCAGAAGAGATCCGCATGACTAAG
我想得到四个整数(用空格分隔),分别计算符号“A”、“C”、“G”和“T”在 s 中出现的次数。
所以我在做
<?
$dna = "ATGGTCCCCTCTTGCCGCGGAAATAATTCCGATATGACCATGGGTAATACTCAAATAATGTAGTTGTGGGAGAGGTATCCACCGTCGGTAGATACTCCTCCGAGCGCTGGTTGGATGAGAGGTTTGTGTGCTTATATTACCGTGAAGCACAGGATCCAAGCCCCAGAGTCAGACCGTCATGTTTGCTTCCGCTGACCGATTACAGCGCTGGAACGTTATAAAGCGCCCACATATTAAGGCACATGACGCTCTCGTAGTTATTTGGGCCGTAATAAATCCAGGGTCTATTTAGCTCGCGCGAGTTTGCAGTGGGCCGACACTAGCAGTTTTGTTCGTAGAGACCTGGCCGAATATTGGCCTGACGAGAAAAGAAGGTGACCACACAATGTAACAGTTCCATATACACCGCACAAAGGGTCATATTATTACCGCCACAACTAGTCCTATCATCTCTGCTTTATCGAATCCAGGGGCAAGAAAAAGTACTGTAGAGTTACCCCGGGTCGGATATACAATGCCGGAAGTGCGTATCGCTACACTCAAGGCCACCCGATACGTCTCCAGCAAGCGGTGGTTGGGGCTGCCTTCAGATGTGTACGTTTCGTGGCAAAGCCTGCTTATATGGTGTTTAATCCAATCGTAGAGAAGGGCGAACCACGATACTGAGCCGACTCGATACGTTGCGGCGAGGCCGTAGCTCCTTTGGGAGTAAGTACAATCGTACACGTGTTAGGCTCTCCCAATATGTCGTAAATCAAACGAAGTATCCAATGGCCTTCCATAAGCCCACCGTCGTCGCATATTAAGGTAGCAGAAGAGATCCGCATGACTAAG";
echo substr_count($dna, 'A') . " " .substr_count($dna, 'C') . " " . substr_count($dna, 'G') . " " . substr_count($dna, 'T');
?>
但我想改进答案,比如在 perl
perl -ne '$,=" ";print y/A//, y/C//, y/G//, y/T//'
或者像在 scala 中那样做一个有效的循环:
var a,c,g,u = 0
s.foreach {
case 'A' => a+=1
case 'C' => c+=1
case 'G' => g+=1
case 'U' => u+=1
}
是for循环的最佳选择还是我可以修改php的函数substr_count
?