#I cant find the problem

122 messages · Page 1 of 1 (latest)

coral wave
#
public class DNA {

    public static void main(String[] args) {
        
        int count = 0;
        
        
        //  Checks that the input given is correct
        if(args.length < 1) {
            System.out.println( "Wrong input!" );
            System.out.println( "Expected input: <strand1> <strand2> ..." );
            return;
        }
        
        //  Initialize the DNA molecule
        String dnaMolecule = "";
        
        //  Finds the first valid strand and saves it as the DNA molecule
        for(int i=0; i<args.length; i++) {
            
            boolean isValid = true;
            String currentStrand = args[i];
            count++;
            
            for(int j=0; j<currentStrand.length(); j++) {
                
                char c = currentStrand.charAt(j);
                
                if(c != 'a' && c != 'c' && c != 'g' && c != 't') {
                    isValid=false;
                    break;
                }
            }
            if(isValid == true) {
                dnaMolecule = currentStrand;
                break;
            }
        }
surreal muskBOT
#

This post has been reserved for your question.

Hey @coral wave! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

coral wave
#
 int maxOverlap = 0;
            
            for (int z=dnaMolecule.length()-1; z>=0; z--) {
                
                overlap = 0;
                
                for (int j=0; j<dnaMolecule.length()-z;j++) {
                    
                    if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                        overlap++;
                        
                    } else {
                        break;
                    }
                    
                }
                
                if (overlap > maxOverlap && overlap >= 4) {
                    maxOverlap = overlap;
                }
                
            }
            
            for (int y=maxOverlap; y<currentStrand.length(); y++) {
                    dnaMolecule += currentStrand.charAt(y);
            }
                    
                if( overlap == 0) {
                    dnaMolecule += currentStrand;
                }
                
            }
        
        System.out.println( "DNA molecule: " + dnaMolecule);
        
    }

}

#

i cant seem to find the problem in this code

#

i get the worng outputs if anyone could let me know why i would apreciate it

potent bluff
#

What input are you using? What output do you expect? What output are you actually getting? Why are you expecting the output you are expecting?

coral wave
# potent bluff What input are you using? What output do you expect? What output are you actuall...

input given is: atga tgagggg atgaacggtagcctgagcta gctah gctaggggg
expected output is: atgaacggtagcctgagctaggggg
the output i am getting is: atgatgaggggtgaggggacggtagcctgagcta
the program receives a series of strigs. It finds the first valid string and saves it as the dnaMolecule. After it compares the DNA molecule with the next valid string. If it finds an overlap of 4 or more characters it adds the next string to the DNA molecule without the overlap.

#

there are a few other inputs and expected outputs but if 1 works then all other will

potent bluff
#

so essentially the output should start earlier?

#

How do you know whether a string is valid?

#

only containing ACGT?

#

what's the overlap thing about?

#

is it that the end of the previous sequence needs to be the same with the beginning of the next sequence?

#

I don't understand how the input relates to the expected output

coral wave
coral wave
#

Some thing like this
a t c g g t a t a c t g
g t a t a c t g g a c t

#

a t c g g t a t a c t g + g a c t = a t c g g t a t a c t g g a c t

#

This is an input example with only 2 strings

potent bluff
#

does it need to be the end of the first string and the beginning of the second string?

#

or can it be a different part of the strings?

coral wave
#

And j cant use arrays or any other functions other than main and args table

potent bluff
#

what should be done if there is no overlap?

#

Did you try using a debugger?

#

What's the point of the break; if isValid is true?

#

oh it's just the first valid one

coral wave
#

If the overlap is 1 2 or 3 it discards the strand

potent bluff
#

Did you really post the whole code here?

#

It seems like you might have missed a loop at the beginning of the second snippet

coral wave
#

1 sec let me check

#

I did

#

Damn my bad

#

Do you want me to send you the full code?

potent bluff
#

yeah, just send it again here

coral wave
#

Ok give me 5 mins

potent bluff
#

btw why is the tgagggg skipped in your expected output?

#

and the gctah is probably skipped because it isn't valid as well?

coral wave
#

Tgagggg is skipped cause the overlap is 3

potent bluff
#

oh so only elements with an overlap should be added

#

btw for diagnosing the issue, it might help writing it down in a different format:

input:              atga tgagggg     atgaacggtagcctgagcta gctah gctaggggg
actual:             atga tgagggg  tgaggggacggtagcctgagcta
expected:           atga             acggtagcctgagcta               ggggg
#

I have modified the spaces here

coral wave
#

No

#

If there is no overlap it adds the entire strand

potent bluff
coral wave
#

If the overlap is 1, 2 or 3 it does not add them

#

Only if the overlap is 4 or longer

potent bluff
#

oh ok

coral wave
#

Let me send the full code again

potent bluff
#

you have that

                if (overlap > maxOverlap && overlap >= 4) {
                    maxOverlap = overlap;
                }
#

that treats all overlaps of a length of less than 4 the same as overlaps of length 0

#

I would skip the overlap >= 4 check here and then only add the strand if the overlap is either 0 or >= 4

coral wave
#

yes

#

thats the point

#

do you want pics of the code or snippets?

potent bluff
potent bluff
potent bluff
#

So you want to treat overlaps of length <4 the same as no overlaps?

#

wouldn't that mean they would still be added?

coral wave
#

wait

#

what do you mean

#

if the overlap is 0 it adds the entire strand

#

there is a sepparate if that says if the overlap is 0 add the entire strand

potent bluff
#

yeah but if the overlap is never >=4, maxOverlap will be 0

coral wave
#

public class DNA {

    public static void main(String[] args) {
        
        int count = 0;
        
        
        //  Checks that the input given is correct
        if(args.length < 1) {
            System.out.println( "Wrong input!" );
            System.out.println( "Expected input: <strand1> <strand2> ..." );
            return;
        }
        
        //  Initialize the DNA molecule
        String dnaMolecule = "";
        
        //  Finds the first valid strand and saves it as the DNA molecule
        for(int i=0; i<args.length; i++) {
            
            boolean isValid = true;
            String currentStrand = args[i];
            count++;
            
            for(int j=0; j<currentStrand.length(); j++) {
                
                char c = currentStrand.charAt(j);
                
                if(c != 'a' && c != 'c' && c != 'g' && c != 't') {
                    isValid=false;
                    break;
                }
            }
            if(isValid == true) {
                dnaMolecule = currentStrand;
                break;
            }
        }
        
        //  If no valid strand is found then print a Wrong Input messege
        if(dnaMolecule == "") {
            System.out.println( "Wrong input" );
            return;
        }
#
//  Create the DNA molecule
        for(int i=count; i<args.length;  i++) {
            
            String currentStrand = args[i];
            int overlap = 0;
            boolean isValid = true;
            
            // Performs the same check to ensure there are no invalid strands after the first valid one
            for (int j=0; j<currentStrand.length(); j++) {
                char c = currentStrand.charAt(j);
                if (c != 'a' && c != 'c' && c != 'g' && c != 't') {
                    isValid = false;
                    break;
                }
            }
            
            // If any invalid strands are found discard them and continue to the next strand
            if(isValid == false) {
                break;
            }
            
            int maxOverlap = 0;
            
            for (int z=dnaMolecule.length()-1; z>=0; z--) {
                
                overlap = 0;
                
                for (int j=0; j<dnaMolecule.length()-z;j++) {
                    
                    if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                        overlap++;
                        
                    } else {
                        break;
                    }
                    
                }
                
                if (overlap > maxOverlap && overlap >= 4) {
                    maxOverlap = overlap;
                }
                
            }
            
            for (int y=maxOverlap; y<currentStrand.length(); y++) {
                    dnaMolecule += currentStrand.charAt(y);
            }
                    
                if( overlap == 0) {
                    dnaMolecule += currentStrand;
                }
                
            }
        
        System.out.println( "DNA molecule: " + dnaMolecule);
        
    }

}
#

here is the full code

potent bluff
#

ok so at the beginning it finds the first valid strand

#

then it starts at the first strand again?

coral wave
#

no

potent bluff
#

oh nvm it starts at count

coral wave
#

yeah

potent bluff
#

in your second code snippet you have

            if(isValid == false) {
                break;
            }
#

What do you expect it to do?

#

I think this will completely stop your program

#

well not the entire program

#

but as soon as it finds an invalid strand, it will go straight to System.out.println( "DNA molecule: " + dnaMolecule);

coral wave
#

wait

#

you are right but that doesnt fix it

#

the output i get now is atgatgaggggtgaggggacggtagcctgagctagctahgctahgggggctaggggg

potent bluff
#

the other issue I found is the thing with maxOverlap

coral wave
#

what is that issue

coral wave
#

i put the if statement that you mentiont inside the next for loop

potent bluff
#

I think you want to change break; to continue;

coral wave
#

i cant use continue

potent bluff
#

instead of moving it into a loop

potent bluff
coral wave
#

yeah

potent bluff
#

Why not?*

coral wave
#

its part of the instructions

potent bluff
#

then use if

coral wave
#

wdym

potent bluff
#

put the whole thing after the isValid check into if(isValid)

#
//  Create the DNA molecule
        for(int i=count; i<args.length;  i++) {
            
            String currentStrand = args[i];
            int overlap = 0;
            boolean isValid = true;
            
            // Performs the same check to ensure there are no invalid strands after the first valid one
            for (int j=0; j<currentStrand.length(); j++) {
                char c = currentStrand.charAt(j);
                if (c != 'a' && c != 'c' && c != 'g' && c != 't') {
                    isValid = false;
                    break;
                }
            }
            
            // If any invalid strands are found discard them and continue to the next strand
            if(isValid) {
            
            int maxOverlap = 0;
            
            for (int z=dnaMolecule.length()-1; z>=0; z--) {
                
                overlap = 0;
                
                for (int j=0; j<dnaMolecule.length()-z;j++) {
                    
                    if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                        overlap++;
                        
                    } else {
                        break;
                    }
                    
                }
                
                if (overlap > maxOverlap && overlap >= 4) {
                    maxOverlap = overlap;
                }
                
            }
            
            for (int y=maxOverlap; y<currentStrand.length(); y++) {
                    dnaMolecule += currentStrand.charAt(y);
            }
                    
                if( overlap == 0) {
                    dnaMolecule += currentStrand;
                }
                
            }
            }
        
        System.out.println( "DNA molecule: " + dnaMolecule);
        
    }

}
#

sorry for the missing indentation but I changed it in Discord

#

actually

coral wave
#

ooh ok

potent bluff
#
//  Create the DNA molecule
        for(int i=count; i<args.length;  i++) {
            
            String currentStrand = args[i];
            int overlap = 0;
            boolean isValid = true;
            
            // Performs the same check to ensure there are no invalid strands after the first valid one
            for (int j=0; j<currentStrand.length(); j++) {
                char c = currentStrand.charAt(j);
                if (c != 'a' && c != 'c' && c != 'g' && c != 't') {
                    isValid = false;
                    break;
                }
            }
            
            // If any invalid strands are found discard them and continue to the next strand
            if(isValid) {
            
                int maxOverlap = 0;
                
                for (int z=dnaMolecule.length()-1; z>=0; z--) {
                    
                    overlap = 0;
                    
                    for (int j=0; j<dnaMolecule.length()-z;j++) {
                        
                        if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                            overlap++;
                            
                        } else {
                            break;
                        }
                        
                    }
                    
                    if (overlap > maxOverlap && overlap >= 4) {
                        maxOverlap = overlap;
                    }
                    
                }
                
                for (int y=maxOverlap; y<currentStrand.length(); y++) {
                        dnaMolecule += currentStrand.charAt(y);
                }
                        
                    if( overlap == 0) {
                        dnaMolecule += currentStrand;
                    }
                    
                }
            }
        
        System.out.println( "DNA molecule: " + dnaMolecule);
        
    }

}
#

that's the right indentation

#

Do you know the difference?

#

btw if(isValid) is the same as if (isValid == true)

coral wave
#

yeah

#

still the same output

potent bluff
#

you still have the other issue

#

with maxOverlap

#

you have this code:

                int maxOverlap = 0;
                
                for (int z=dnaMolecule.length()-1; z>=0; z--) {
                    
                    overlap = 0;
                    
                    for (int j=0; j<dnaMolecule.length()-z;j++) {
                        
                        if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                            overlap++;
                            
                        } else {
                            break;
                        }
                        
                    }
                    
                    if (overlap > maxOverlap && overlap >= 4) {
                        maxOverlap = overlap;
                    }
                    
                }
#

The only purpose of that is finding the max overlap, right?

coral wave
#
for (int z=dnaMolecule.length()-1; z>=0; z--) {
                
                    overlap = 0;
                
                    for (int j=0; j<dnaMolecule.length()-z;j++) {
                    
                        if (currentStrand.charAt(j) == dnaMolecule.charAt(z+j)) {
                            overlap++;
                        }else {
                            break;
                        }
                    
                    }
                
                    if (overlap > maxOverlap && overlap >= 4) {
                        maxOverlap = overlap;
                    }
                
                }
            
                for (int y=maxOverlap; y<currentStrand.length(); y++) {
                    dnaMolecule += currentStrand.charAt(y);
                }
#

i think the problem is this part

potent bluff
#

So, what will maxOverlap be if you have an overlap of 2 characters?

coral wave
#

0

potent bluff
#

What will this code do if maxOverlap is 0?

                for (int y=maxOverlap; y<currentStrand.length(); y++) {
                        dnaMolecule += currentStrand.charAt(y);
                }
                        
                    if( overlap == 0) {
                        dnaMolecule += currentStrand;
                    }
                    
                }
coral wave
#

oh wait

#

found it

potent bluff
#

nice

coral wave
#

ty for helpinh