Home > Net >  Array seems to be mixing content
Array seems to be mixing content

Time:09-11

I'm quite new to coding so please bare with me.

I'm attempting a Java challenge and have run into an issue. The challenge involves breaking down a large string into smaller strings of consisting 3 characters, then returning the value that relates to these 3 characters.

The for loop is supposed to check and see if rnaSequence matches a key in rnaHashMap if it does it adds the value from rnaHashMap to finalList

import java.util.*;
import java.util.stream.Stream;

class ProteinTranslator {
    static HashMap<String, String> rnaHashMap = new HashMap<String, String>();
    List<String> finalList = new ArrayList<>();
    static {
       rnaHashMap.put("AUG", "Methionine");

       rnaHashMap.put("UUU", "Phenylalanine");
       rnaHashMap.put("UUC", "Phenylalanine");

       rnaHashMap.put("UUA", "Leucine");
       rnaHashMap.put("UUG", "Leucine");

       rnaHashMap.put("UCU", "Serine");
       rnaHashMap.put("UCC", "Serine");
       rnaHashMap.put("UCA", "Serine");
       rnaHashMap.put("UCG", "Serine");

       rnaHashMap.put("UAU", "Tyrosine");
       rnaHashMap.put("UAC", "Tyrosine");

       rnaHashMap.put("UGU", "Cysteine");
       rnaHashMap.put("UGC", "Cysteine");

       rnaHashMap.put("UGG", "Tryptophan");

       rnaHashMap.put("UAA", "STOP");
       rnaHashMap.put("UAG", "STOP");
       rnaHashMap.put("UGA ", "STOP");
    }

    List<String> translate(String rnaSequence) {

        List<String> split = List.of(rnaSequence.split("(?<=\\G.{3})"));

        for(Map.Entry m: rnaHashMap.entrySet()) {
             if (split.contains(m.getKey())){
                 if (m.getValue().equals("STOP") || finalList.size() >= 3) {
                     return finalList;
                 } else {
                     finalList.add((String) m.getValue());
                 }
            }
        }
        return finalList;
    }

}

After the "STOP" value is reached current values should be returned but nothing past that. When I run the tests the result is.


expected:<[Tryptophan, Cysteine, Tyrosine]> but was:<[Cysteine, Tyrosine, Phenylalanine]>
Expected :[Tryptophan, Cysteine, Tyrosine]
Actual   :[Cysteine, Tyrosine, Phenylalanine]

I'm thinking that one of these lists is maybe mixing the order around, any ideas?

CodePudding user response:

Your for loop is iterating through the wrong collection. It's iterating through the rnaHashMap, and the resulting amino acid list will be in the order of the key map from that map. Also, duplicate RNA triplets won't be translated as you're currently doing it. Instead, you should be iterating through the split List, the list or RNA triplets, in order of the tokens it contains, extracting the amino acid map entry for each item in the split list.

Also, shouldn't finalList be declared and initialized within the translate(...) method?

e.g..,

public List<String> translate(String rnaSequence) {
    List<String> finalList = new ArrayList<>();
    List<String> split = List.of(rnaSequence.split("(?<=\\G.{3})"));

    for (String key: split) {
        String value = rnaHashMap.get(key);
        if (value.equals("STOP") || finalList.size() >= 3) {
            break;
        } else {
            finalList.add();
        }
    }
    return finalList;
}
Note: Code not tested

CodePudding user response:

Try with this code:

List<String> translate(String rnaSequence) {
    List<String> res = new ArrayList<String>();
    StringBuilder sb = new StringBuilder(rnaSequence);
    if(sb.length() > 3) {
        for(int i = 3; i < sb.length(); i =4) {
            sb.insert(i, '-');          
        }
        res = Arrays.asList(sb.toString().split("-"));
    }else {
        res.add(rnaSequence);
    }
    return changeCodonsForProtein(res);
}

List<String> changeCodonsForProtein(List<String> list){
    List<String> res = new ArrayList<String>();
    for(String s: list) {
        if(s.equals("AUG")){
            res.add("Methionine");
        }else if(s.equals("UUU") || s.equals("UUC")) {
            res.add("Phenylalanine");
        }else if(s.equals("UUA") || s.equals("UUG")) {
            res.add("Leucine");
        }else if(s.equals("UCU") || s.equals("UCC") || s.equals("UCA") || s.equals("UCG")) {
            res.add("Serine");
        }else if(s.equals("UAU") || s.equals("UAC")) {
            res.add("Tyrosine");
        }else if(s.equals("UGU") || s.equals("UGC")) {
            res.add("Cysteine");
        }else if(s.equals("UGG")) {
            res.add("Tryptophan");
        }else if(s.equals("UAA") || s.equals("UAG") || s.equals("UGA")) {   
            break;
        }
    }
    return res;
}

I tested it and it works:

public static void main(String[] args) {

    ProteinTranslator pt = new ProteinTranslator();
    String s = "AUGUUUUCU";
    
    System.out.println(pt.translate(s));
    
}

the output was like expected:

[Methionine, Phenylalanine, Serine]

  • Related