We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In my code, I thought of simply finding the position at which the number of gene characters exceeds the limit. I do it from both front and back side then find the length of part of DNA which is to be removed. I find nothing wrong with my logic but the code still doesn't work. What am i doing wrong?
/*
* Complete the 'steadyGene' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING gene as parameter.
*/
int steadyGene(string gene)
{
int s = 0, l = gene.length();
int a, t, g, c, max = gene.length() / 4;
a = t = g = c = 0;
for (int i = 0; i < gene.length(); i++)
{
if (gene[i] == 'A')
a++;
if (gene[i] == 'T')
t++;
if (gene[i] == 'G')
g++;
if (gene[i] == 'C')
c++;
if (a > max || t > max || g > max || c > max)
{
s = i;
break;
}
}
for (int i = gene.length() - 1; i >= 0; i--)
{
if (gene[i] == 'A')
a++;
if (gene[i] == 'T')
t++;
if (gene[i] == 'G')
g++;
if (gene[i] == 'C')
c++;
if ((gene[i] == 'A' && a > max) || (gene[i] == 'T' && t > max) || (gene[i] == 'G' && g > max) || (gene[i] == 'C' && c > max))
{
l = i;
break;
}
}
int k1 = l - s + 1;
a = t = g = c = 0;
for (int i = gene.length() - 1; i >= 0; i--)
{
if (gene[i] == 'A')
a++;
if (gene[i] == 'T')
t++;
if (gene[i] == 'G')
g++;
if (gene[i] == 'C')
c++;
if (a > max || t > max || g > max || c > max)
{
l = i;
break;
}
}
for (int i = 0; i < gene.length(); i++)
{
if (gene[i] == 'A')
a++;
if (gene[i] == 'T')
t++;
if (gene[i] == 'G')
g++;
if (gene[i] == 'C')
c++;
if ((gene[i] == 'A' && a > max) || (gene[i] == 'T' && t > max) || (gene[i] == 'G' && g > max) || (gene[i] == 'C' && c > max))
{
s = i;
break;
}
}
int k2 = l - s + 1;
return min(k1, k2);
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string n_temp;
getline(cin, n_temp);
int n = stoi(ltrim(rtrim(n_temp)));
string gene;
getline(cin, gene);
int result = steadyGene(gene);
fout << result << "\n";
fout.close();
return 0;
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →
In my code, I thought of simply finding the position at which the number of gene characters exceeds the limit. I do it from both front and back side then find the length of part of DNA which is to be removed. I find nothing wrong with my logic but the code still doesn't work. What am i doing wrong?
include
using namespace std;
string ltrim(const string &); string rtrim(const string &);
/* * Complete the 'steadyGene' function below. * * The function is expected to return an INTEGER. * The function accepts STRING gene as parameter. */
int steadyGene(string gene) {
}
int main() { ofstream fout(getenv("OUTPUT_PATH"));
}
string ltrim(const string &str) { string s(str);
}
string rtrim(const string &str) { string s(str);
}