【发布时间】:2018-01-19 01:26:24
【问题描述】:
给定一个字符串作为输入,我需要将 a's 更改为 t's,c's 更改为 g's,u's 更改为 a's,g's 更改为 c's [a-t, c-g, g-c, u-a]。另一个特殊情况是最多有两种情况 u's 转换为 g's 和/或 g's 转换为 t's [u-g, g-t]。
作为例子 -
input :
auugca
output :
taacgt
tgacgt
taatgt
tggcgt
tgatgt
tagtgt
在输出中,
- 无特殊变化
- 将第二个字符 u 特殊更改为 g
- 将第三个字符 u 特殊更改为 g
- 将第 4 个字符 g 特殊更改为 t
- 第二个和第三个字符的两个特殊变化
- 第 2 和第 4 个字符的两个特殊变化
- 第 3 和第 4 个字符的两个特殊变化
我是这样想的,
- 从字符串的开头开始
- 转换 a-t 和 c-g。
- 如果找到 u 或 g,检查之前有多少特价商品 遇到,如果小于两个,则视为特殊,递增 以前遇到的计数器,从字符串的下一个位置递归创建两个新搜索,一个有特殊考虑,其他没有
我想出的代码是(在 Python 中) -
# mirna is the string to be converted
# wobblecount is the number of specials coneverted
# location is from which location of the string the convertion will start
# compliment is the end string to be created
def createonewobble(mirna, location, wobblecount, compliment):
for counter in range(location, len(mirna)):
if (mirna[counter] == 'A' or mirna[counter] == 'a'):
compliment = compliment + "t"
elif (mirna[counter] == 'C' or mirna[counter] == 'c'):
compliment = compliment + "g"
elif ((mirna[counter] == 'U' or mirna[counter] == 'u') and (wobblecount < 2)):
compliment = compliment + "g"
createonewobble(mirna, counter+1 , wobblecount+1, compliment)
compliment = compliment + "a"
createonewobble(mirna, counter+1 , wobblecount, compliment)
elif ((mirna[counter] == 'G' or mirna[counter] == 'g') and (wobblecount < 2)):
compliment = compliment + "t"
createonewobble(mirna, counter+1 , wobblecount+1, compliment)
compliment = compliment + "a"
createonewobble(mirna, counter+1 , wobblecount, compliment)
elif ((mirna[counter] == 'U' or mirna[counter] == 'u') and (wobblecount == 2)):
compliment = compliment + "a"
elif ((mirna[counter] == 'G' or mirna[counter] == 'g') and (wobblecount == 2)):
compliment = compliment + "c"
print compliment
mirna = "auugca"
createonewobble(mirna, 0, 0, "")
输出
tggcgt
tggatgt
tggatagt
tggatagt
tggatgt
tggatagt
tggatagt
tgagtgt
tgagtagt
tgagtagt
tgagatgt
tgagatagt
tgagatagt
tgagatgt
tgagatagt
tgagatagt
tgagtgt
tgagtagt
tgagtagt
tgagatgt
tgagatagt
tgagatagt
tgagatgt
tgagatagt
tgagatagt
这给了我 25 个输出,没有一个正确的输出,而且有些输出的长度远远超过字符串的大小。我哪里错了?
【问题讨论】:
-
您的代码引用了
createonewobble,但没有定义它。请创建演示问题的最短的完整程序。请将该程序以及预期和实际输出复制粘贴(切勿重新输入)到您的问题中。请参阅minimal reproducible example 了解更多信息。 -
@Rob,抱歉,我更改了要转换的函数的名称以使其更具可读性,但在递归调用中忘记了这样做。再次抱歉。
-
请按现在的样子运行代码,并将实际和预期的结果复制粘贴到您的问题中。
-
你的逻辑有问题。除了不小心使用
convert作为函数名称而不是createonewobble的明显拼写错误之外,请考虑每次遇到'g'或'u'时,除了您创建的搜索之外,您还创建了两个新搜索'目前正在运行。这就是为什么你会得到很多额外的东西。您可以通过在进入循环之前执行lowmirna = mirna.lower()之类的操作并消除近一半的比较,并使用列表来跟踪赞美而不是打印它们,从而清楚地知道来自哪里的内容,可以使事情变得更加清晰。跨度> -
@Feneric,我不明白你要我对字符串的后半部分做什么。