Hi, I got a WA in 385 (DNA Translation) and cannot find out why. My algorithm is straightforward, I go forward and if I cannot find a valid primary or complentary strand then I go backward.
Can someone give me a test case to show that my program is wrong?
/* @JUDGE_ID: 39439PK 385 C */
#include <stdio.h>
#define NUM_BASES 4
#define MAX_LEN 256
#define CODON_LEN 3
#define ph "Phe"
#define le "Leu"
#define se "Ser"
#define ty "Tyr"
#define cy "Cys"
#define tr "Trp"
#define pr "Pro"
#define hi "His"
#define gl "Gln"
#define ar "Arg"
#define il "Ile"
#define me "Met"
#define th "Thr"
#define as "Asn"
#define ly "Lys"
#define va "Val"
#define al "Ala"
#define ap "Asp"
#define gu "Glu"
#define gy "Gly"
/* Amino Acid table lookup */
char *aminoAcid[NUM_BASES][NUM_BASES][NUM_BASES] = {
{{ph,ph,le,le}, {se,se,se,se}, {ty,ty,"",""},{cy,cy,"",tr}},
{{le,le,le,le}, {pr,pr,pr,pr}, {hi,hi,gl,gl},{ar,ar,ar,ar}},
{{il,il,il,me}, {th,th,th,th}, {as,as,ly,ly},{se,se,ar,ar}},
{{va,va,va,va}, {al,al,al,al}, {ap,ap,gu,gu},{gy,gy,gy,gy}},
};
/* index to Amino acid table */
#define U 0
#define C 1
#define A 2
#define G 3
int findStartCodon(char *s) {
int n = CODON_LEN;
return strncmp(s,"TAC",n)==0 || strncmp(s,"ATG",n)==0;
}
int findStopCodon(char *s, int isPrimary) {
int n = CODON_LEN;
if (isPrimary) {
return strncmp(s,"ATT",n)==0 || strncmp(s,"ATC",n)==0 ||
strncmp(s,"ACT",n)==0;
}
else {
return strncmp(s,"TAA",n)==0 || strncmp(s,"TAG",n)==0 ||
strncmp(s,"TGA",n)==0;
}
}
struct {
char *seq[MAX_LEN/CODON_LEN];
int len;
} proteinSeq;
int getline(char s[], int lim) {
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n') {
s[i++] = c;
}
s[i] = '\0';
if (c == EOF) return -1;
return i;
}
void printProteinSeq() {
int i;
for (i = 0; i < proteinSeq.len; i++) {
printf("%s", proteinSeq.seq[i]);
if (i < proteinSeq.len-1) printf("-");
}
printf("\n");
}
int baseConvert(char base, int isPrimary) {
switch(base) {
case 'A':
return isPrimary ? U : A;
case 'T':
return isPrimary ? A : U;
case 'G':
return isPrimary ? C : G;
case 'C':
return isPrimary ? G : C;
}
}
/* Generate Protein sequence for a forward order DNA strand */
int processForward(char *s) {
int i, j, sLen = strlen(s);
int idx[CODON_LEN]; /* index to aminoAcid lookup */
char *amino;
int isPrimary;
proteinSeq.len = 0; /* Number of amino acids in protein */
if (sLen <= CODON_LEN) return 0;
isPrimary = s[0] == 'T' ? 1 : 0;
for (i = CODON_LEN; i <= sLen-CODON_LEN; i += CODON_LEN) {
if (findStopCodon(&s[i], isPrimary)) break;
for (j = 0; j < CODON_LEN; j++) {
idx[j] = baseConvert(s[i+j], isPrimary);
}
amino = aminoAcid[idx[0]][idx[1]][idx[2]];
if (strlen(amino)==0) return 0;
proteinSeq.seq[proteinSeq.len++] = amino;
}
if (proteinSeq.len == 0 || i > sLen-CODON_LEN) {
return 0;
}
else {
printProteinSeq();
return 1;
}
}
int main() {
int i, sLen;
int goBackward = 0;
char DNAStrand[MAX_LEN];
char *s, *start, *end, c;
/* Read DNA strand */
while (getline(DNAStrand, MAX_LEN) != -1) {
if (strcmp(DNAStrand, "*") == 0) break;
s = DNAStrand;
goBackward = 0;
while (*s != '\0') {
if ( findStartCodon(s) && processForward(s) ) {
break;
}
s++;
if (*s == '\0' && !goBackward) {
goBackward = !goBackward;
/* Reverse DNA strand */
for (start=DNAStrand, end=s-1; start < end; start++, end--) {
c = *start;
*start = *end;
*end = c;
}
s = DNAStrand;
}
}
if (*s=='\0') printf("*** No translatable DNA found ***\n");
}
}

