Kemat Rochi Kemat Rochi - 1 day ago 3
Java Question

How to check for conditions dynamically in java?

I am trying to create a list of all possible two letter words using 4 letters - A, C, T, G. So the output should be like

Two letter word output

AA
CA
TA
GA
AC
CC
TC
GC
AT
CT
TT
GT
AG
CG
TG
GG


I am able to produce the above output using this Java code

Two letter word Java code

import java.io.IOException;

class Variations {

private static char replace (char bc) {
switch (bc) {
case 'A':
bc = 'C';
break;

case 'C':
bc = 'T';
break;

case 'T':
bc = 'G';
break;

case 'G':
bc = 'A';
break;
}

return bc;
}

public static void main(final String[] args) throws IOException {

String code = "AA";
char[] bc = new char[code.length()];

for (int i=0; i<code.length(); i++) {
bc[i] = code.charAt(i);
}


for (int i=0; i < Math.pow(4, bc.length); i++) {

System.out.println(bc);

bc[0] = replace(bc[0]);

if (i%4 == 0) {
bc[1] = replace(bc[1]);
}


}

}
}


Now I want to create a list of all possible three letter words using the same 4 letters - A, C, T, G. So the output will be like

Three letter word output

AAA
CAA
TAA
GAA
ACA
CCA
TCA
GCA
ATA
CTA
TTA
GTA
AGA
CGA
TGA
GGA
AAC
CAC
TAC
GAC
ACC
CCC
TCC
GCC
ATC
CTC
TTC
GTC
AGC
CGC
TGC
GGC
AAT
CAT
TAT
GAT
ACT
CCT
TCT
GCT
ATT
CTT
TTT
GTT
AGT
CGT
TGT
GGT
AAG
CAG
TAG
GAG
ACG
CCG
TCG
GCG
ATG
CTG
TTG
GTG
AGG
CGG
TGG
GGG


So to generate this above output, I just have to add one more
if
statement in my main method,

Three letter word Java code

import java.io.IOException;

class Variations {

private static char replace (char bc) {
switch (bc) {
case 'A':
bc = 'C';
break;

case 'C':
bc = 'T';
break;

case 'T':
bc = 'G';
break;

case 'G':
bc = 'A';
break;
}

return bc;
}

public static void main(final String[] args) throws IOException {

String code = "AAA";
char[] bc = new char[code.length()];

for (int i=0; i<code.length(); i++) {
bc[i] = code.charAt(i);
}


for (int i=0; i < Math.pow(4, bc.length); i++) {

System.out.println(bc);

bc[0] = replace(bc[0]);

if (i%4 == 0) {
bc[1] = replace(bc[1]);
}

if (i%16 == 0) {
bc[2] = replace(bc[2]);
}


}

}
}


So my question is, if the user has a choice of specifying the whether he/she wants a list of all 2-letter words or 3-letter words - how do I combine both the programs? Both programs vary by just one conditional statement.

The user may specify if he/she needs a list of all possible 2-letter words or 3-letter words or even 4-letter words. My program has to dynamically add or remove a conditional statement to generate the list. Is it possible to do this in Java?

Thank you in advance!

Answer

You are basically implementing a cartesian product. There is a very nice method in Google's Guava called Sets#cartesianProduct.

With this you can easily generate all possible combinations:

Set<List<String>> set = Sets.cartesianProduct(Collections.nCopies(3, ImmutableSet.of("A", "C", "T", "G")));

You can have the 3 as a user-determined parameter.

Now we just need some Java 8 Stream "magic":

String output = set.stream()
    .map(list -> list.stream().collect(Collectors.joining()))
    .collect(Collectors.joining("\n"));

You can of course adjust the last part, it is just to mimic the output you showed in your question.

Comments