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;
}
}
#I cant find the problem
122 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @coral wave! Please use
/closeor theClose Postbutton 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.
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
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?
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
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
Yes its valid if it only has a, c , g, t
Basicaly overlap mean that it compares 2 strings and the maximum amount of charactera that match is the overlap
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
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?
It need to be like that
And j cant use arrays or any other functions other than main and args table
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
If there is no overlap it adds tge hole string to the dna
If the overlap is 1 2 or 3 it discards the strand
Did you really post the whole code here?
It seems like you might have missed a loop at the beginning of the second snippet
yeah, just send it again here
Ok give me 5 mins
btw why is the tgagggg skipped in your expected output?
and the gctah is probably skipped because it isn't valid as well?
Tgagggg is skipped cause the overlap is 3
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
this is what you told me with different spaces
oh ok
Let me send the full code again
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
Do you mean that as "you should change that" or "it should be the way you wrote it"?
Discord codeblocks
it should be as i wrote it
So you want to treat overlaps of length <4 the same as no overlaps?
wouldn't that mean they would still be added?
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
yeah but if the overlap is never >=4, maxOverlap will be 0
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
ok so at the beginning it finds the first valid strand
then it starts at the first strand again?
no
oh nvm it starts at count
yeah
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);
wait
you are right but that doesnt fix it
the output i get now is atgatgaggggtgaggggacggtagcctgagctagctahgctahgggggctaggggg
the other issue I found is the thing with maxOverlap
what is that issue
What did you change?
i put the if statement that you mentiont inside the next for loop
I think you want to change break; to continue;
i cant use continue
instead of moving it into a loop
not?
yeah
Why not?*
its part of the instructions
then use if
wdym
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
ooh ok
// 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)
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?
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
yeah
So, what will maxOverlap be if you have an overlap of 2 characters?
0
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;
}
}
nice
ty for helpinh