Exercism————Protein Translation
程序员文章站
2022-04-19 17:17:35
...
题目:
最优解法:
package practice;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
class ProteinTranslator
{
static final String STOP = "STOP";
HashMap<String,String> proteinMap = new HashMap<>();
//@Constructor
public ProteinTranslator() {
multiPut("Methionine", "AUG");
multiPut("Phenylalanine", "UUU", "UUC");
multiPut("Leucine", "UUA", "UUG");
multiPut("Serine", "UCU", "UCC", "UCA", "UCG");
multiPut("Tyrosine", "UAU", "UAC");
multiPut("Cysteine", "UGU", "UGC");
multiPut("Tryptophan","UGG");
multiPut(STOP, "UAA", "UAG", "UGA");
}
// Set one value to multikeys
void multiPut(String value,String ... keys) {
for(String key : keys) {
proteinMap.put(key,value);
}
}
// translate the RNAChain
List<String> translate(String RNAChain) throws IllegalArgumentException{
ArrayList<String> proteins = new ArrayList<>();
String codon,protein;
for(int i = 0;i < RNAChain.length()/3;i += 3) {
codon = RNAChain.substring(i,i+3);
protein = proteinMap.get(codon);
if(codon == STOP) {break;}
if(protein == null) {throw new IllegalArgumentException("The condon must be condon");}
proteins.add(protein);
}
return proteins;
}
}
解析:
利用HaspMap多对一的关系,将codon和protein放进HashMap中,**“multput”**方法是本解法的亮点,再利用for翻译,最后将结果放进ArrayList中
推荐阅读
-
PHP get_html_translation_table()函数用法讲解
-
JavaScript translation for : **.jsp intentionally not implemented
-
[20181015]12C SQL Translation Framework.txt
-
CSS3中translate、transform和translation,和动画animation实例讲解
-
吴恩达 深度学习 编程作业(5-3)Part 1 - Neural Machine Translation
-
Neural Machine Translation (seq2seq) Tutorial----from https://www.tensorflow.org/tutorials/seq2seq
-
keras 2.0:Encoder-Decoder Sequence-to-Sequence Model for Neural Machine Translation
-
论文复现《Effective Adversarial Regularization for Neural Machine Translation》
-
nmt Neural Machine Translation (seq2seq)
-
[coursera/SequenceModels/week3]Neural machine translation with attention[assignment]