本文共 1154 字,大约阅读时间需要 3 分钟。
开始在Codewars刷Python3题,发现基本知识都忘完了,边刷边复习边记录。
8kyu>>7kyu
Deoxyribonucleic acid (DNA) is a chemical found in the nucleus of cells and carries the "instructions" for the development and functioning of living organisms.If you want to know more http://en.wikipedia.org/wiki/DNAIn DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G". You have function with one side of the DNA (string, except for Haskell); you need to get the other complementary side. DNA strand is never empty or there is no DNA at all (again, except for Haskell).DNA_strand ("ATTGC") # return "TAACG"DNA_strand ("GTAT") # return "CATA"
这个题目其实很简单,就是执行一个DNA转RNA的过程,给定一个DNA单链字符串,输出与之对应的RNA字符串。
开始自己想得很复杂了,想着弄成列表,结果输出还要转换成字符串。。。
在这里只需要写一个类似switch的判断就好了,然后再用join()
把每一个转换好的RNA字符链接成字符串输出就好了。
def DNA_strand(dna): # code here my = { "A" : "T", "T" : "A", "C" : "G", "G" : "C", } return "".join(my[i] for i in dna)
代码中my
实现了对输入的DNA字符串进行转换的过程。
join()
用法:
str.join(sequence)
前面的str
指的是使用什么字符来连接需要连接的元素序列sequence
。
""
。 转载地址:http://immws.baihongyu.com/