我们如何使用递归来计算函数中所有长度为n的dna序列。
例如,如果给定函数2,则返回['AA','AC','AT','AG','CA','CC','CT','CG','TA','TC ','TT','TG','GA','GC','GT','GG']
等等...
最佳答案
这是一种方法:
def all_seq(n, curr, e, ways):
"""All possible sequences of size n given elements e.
ARGS
n: size of sequence
curr: a list used for constructing sequences
e: the list of possible elements (could have been a global list instead)
ways: the final list of sequences
"""
if len(curr) == n:
ways.append(''.join(curr))
return
for element in e:
all_seq(n, list(curr) + [element], e, ways)
perms = []
all_seq(2, [], ['A', 'C', 'T', 'G'], perms)
print(perms)
输出:
['AA', 'AC', 'AT', 'AG', 'CA', 'CC', 'CT', 'CG', 'TA', 'TC', 'TT', 'TG', 'GA', 'GC', 'GT', 'GG']
关于python - 创建长度为n的DNA序列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57343891/