从 for 循环的每次迭代创建一个新变量
Create a new variable from each iteration of a for loop
我正在尝试编写一个需要两个字符串的函数:一个是细菌的"基因组",例如ATACGAGA或类似的东西。 另一串是DNA序列,例如必须在基因组中找到的ATA。 该函数应该返回它在将DNA序列与基因组进行比较时发现的最佳相似性分数。 因此,由于 ATA 是 3 个字符,它会将 ATA 与基因组中的前三个字符进行比较,然后是第二、第三和第四个字符,然后是第三、第四和第五个字符,依此类推。 由于ATA是基因组的前三个字符,因此相似性得分为1。 例如,如果我正在寻找的子字符串是 ATG,因为 2/3 的字符相同,它将返回 0.66。
到目前为止,我的函数准确地返回第一个 for 循环每次迭代的 simScore。 例如,如果基因组串是ABCABC,子串是ABC,它会准确地计算i=0处的simScore为1,i=3为1,因为ABC与这些位置的基因组完全匹配。 但是,要找到最佳的simScore,我需要以某种方式找到for循环每次迭代的simScore,并且: 1. 每次迭代都将其与之前的simScore进行比较,并始终保持较高的simScore,或者 2:将每个新的simScore存储在一个新变量中,然后比较所有simScore以确定最高的一个。
double findBestSimScore(string genome, string sequence)
{
double simScore; // similarity score for the genome and the substring
double newSimScore;
double bestSimScore;
int differences = 0;
if(genome == "" || sequence == "")
{
return 0;
}
else if(genome.length() < sequence.length())
{
return 0;
}
else
{
for(int i = 0; i < genome.length(); i++)
{
string sub = genome.substr(i, sequence.length());
differences = 0;
for(int c = 0; c < sequence.length(); c++)
{
if(sub[c] != sequence[c])
{
differences++;
}
}
simScore = (sequence.length() - differences) / (sequence.length() * 1.0); // This calculation needs to be done in each iteration of the first for loop and stored as a new variable, or
// It could also work if each time the simScore is calculated, it is compared to the previous simScore to see if it is larger
}
/*
Need if else statements here that compare each simsScore and determine the highest simScore
*/
return bestSimScore;
}
}
int main()
{
//test 1
//
cout << findBestSimScore("ABCABC", "ABT") << endl;
我会从负位置开始在基因组中移动序列(原始代码中的索引无效(。如果找到更好的bestSimScore = newSimScore > bestSimScore ? newSimScore : bestSimScore;
,则交换最佳分数(这是您的问题((存储在数组中要复杂得多(。循环可以通过1.0
分数停止:
double findBestSimScore(string genome, string sequence)
{
if (genome == "" || sequence == "")
{
return 0;
}
double bestSimScore = 0;
// move virtually the sequence string through the genome starting at a negative position!
for (int i = -(int)sequence.length() + 1; i < (int)genome.length(); i++)
{
int matchCount = 0;
for (int j = 0; j < sequence.length(); j++)
{
int genomeIndex = i + j;
if (genomeIndex >= 0 && genomeIndex < genome.length()) // do not check if positions are out of range ()
{
matchCount += genome[genomeIndex] == sequence[j] ? 1 : 0;
}
}
// calculate new score and compare to previous
double newSimScore = (double)matchCount / (double)sequence.length();
bestSimScore = newSimScore > bestSimScore ? newSimScore : bestSimScore;
if (bestSimScore == 1.0) // if we found a full match: we can stop
{
break;
}
}
return bestSimScore;
}
int main()
{
assert(findBestSimScore("ABCABC", "ABT") == 2.0 / 3.0);
assert(findBestSimScore("ABCABC", "ABC") == 1.0);
assert(findBestSimScore("ABCABC", "BC") == 1.0);
assert(findBestSimScore("ABCABC", "ABCABC") == 1.0);
assert(findBestSimScore("ABCABD", "BDE") == 2.0 / 3.0);
assert(findBestSimScore("BDE", "ABCABD") == 1.0 / 3.0);
return 0;
}
相关文章:
- C++:继续创建新的变量可以吗
- 定义C++新的环境变量并在 bat 文件中使用它
- 堆栈粉碎 在我在代码中添加新变量以及一些操作后C++检测到
- C++,每个循环初始化一个新的静态变量
- 非指针变量和类成员上的新放置
- 从 for 循环的每次迭代创建一个新变量
- 如何从构造函数声明新的私有变量?
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- 有没有办法初始化不涉及编写构造函数的新结构变量?
- 是否可以在没有结构的情况下创建新的类型变量?
- “auto”每次创建一个新的变量实例
- 使用C 中的Cython CDEF公共变量:变量永远不会初始化
- 字符串从变量(&变量)地址的长度 - 嵌入式C
- C++ 结构中的可选变量(变量结构)
- 用向量替换类新的[]变量-移动、复制运算符
- 我可以在构建一个新的变量模板参数时使用一个变量模板参数吗
- 未初始化的本地变量(变量是类中的对象)-MYSQL
- C共享对象,带有c++归档,静态变量/变量,和开放
- 变量>>变量在 c++ 中的含义是什么?
- 在类中构造新的变量使我的程序崩溃