问题
对于正整数aa和nn,aa modulo nn(简写为amodnamodn)是aa除以nn时的余数。例如,29mod11 = 729mod11 = 7,因为29 = 11×2 + 729 = 11×2 + 7.
模运算是关于模运算的加法,减法,乘法和除法的研究。如果amodn = bmodnamodn = bmodn,我们说aa和bb是一致的模数nn;在这种情况下,我们使用符号a≡bmodna≡bmodn。
模运算中的两个有用的事实是,如果a≡bmodna≡bmodn和c≡dmodnc≡dmodn,则a +c≡b+ dmodna +c≡b+ dmodn和a×c≡b×dmodna×c≡b ×dmodn。为了检查您对这些规则的理解,您可能希望验证这些关系a = 29a = 29,b = 73b = 73,c = 10c = 10,d = 32d = 32,n = 11n = 11.
正如您将在本练习中看到的,一些Rosalind问题将要求一个(非常大的)整数解模数较小的数字,以避免存储这么大数字时出现的计算陷阱。
给定:长度最多为1000 aa的蛋白质串。
返回:蛋白质可以翻译的不同RNA链的总数,模数为1,000,000。 (不要忽视终止密码子在蛋白质翻译中的重要性。)
示例数据集
MA
示例输出
12
我的答案总是设为零。我意识到使用mod的方式存在一些问题,但我不知道究竟是什么。
rna_table = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
"UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
"UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
"UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
"CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
"CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
"CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
"CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
"AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
"ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
"AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
"AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
"GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
"GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
"GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
"GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G"}
with open("rosalind_mrna.txt") as myfile:
data = myfile.readlines()
charData = list(data[0].strip())
frequency_list = {};
for k,v in rna_table.items():
if not frequency_list.has_key(v):
frequency_list[v] = 1
else:
frequency_list[v] += 1
answer = frequency_list['STOP'];
for aa in charData:
answer = ((answer * frequency_list[aa]) % 1000000)
print "Answer is:\n"
print answer % 1000000
答案 0 :(得分:1)
一些小问题:
"UCC":"s"
应该是"UCC":"S"
if not frequency_list.has_key(v):
更像Python编写为if v not in frequency_list:
(实际上.has_key
已从Python 3中删除)charData = list(data[0].strip())
- list()
不需要,您也可以直接迭代字符串frequency_list = {};
- 不需要后面的分号print answer % 1000000
- 最后的% 1000000
是不必要的,它已在循环计算中完成answer
所以看起来示例MA
就像
(
{number of ways to encode M == 1}
* {number of ways to encode A == 4}
* {number of ways to encode STOP == 3}
)
% 1000000
如上所述,给出12。
您的代码按原样运行,返回827968
。我会查看您的rosalind_mrna.txt
副本(确保它不是空白文件)并检查data
的内容(确保它正确加载文件)。
为了比较,我会把它写成
from functools import reduce
from operator import mul
freq = {
'A': 4, 'C': 2, 'D': 2, 'E': 2,
'F': 2, 'G': 4, 'H': 2, 'I': 3,
'K': 2, 'L': 6, 'M': 1, 'N': 2,
'P': 4, 'Q': 2, 'R': 6, 'S': 6,
'T': 4, 'V': 4, 'W': 1, 'Y': 2,
'STOP': 3
}
def load_dna_file(fname):
with open(fname) as inf:
dna = inf.read()
return "".join(dna.split()) # removes all whitespace
def num_rna_strings(dna, modulo=None):
if modulo:
reduce_fn = lambda a, b: (a * b) % modulo
else:
reduce_fn = mul
freqs = (freq[base] for base in dna)
return reduce(reduce_fn, freqs, freq["STOP"])
def main():
dna = load_dna_file("rosalind_mrna.txt")
num = num_rna_strings(dna, 1000000)
print("Answer is {}".format(num))
if __name__ == "__main__":
main()
如果你不感兴趣,如果你不使用modulo,那么完整的答案是423位长(6.186 * 10 ** 422)