设为首页 加入收藏

TOP

使用ROSALIND平台进行Python实战练习(一)
2019-02-17 02:47:12 】 浏览:122
Tags:使用 ROSALIND 平台 进行 Python 实战 练习

使用ROSALIND平台进行Python实战练习


8723194-479b1d12b4d534e4.png
image-20181011163544215.png

Python Village

  • INI1--Installing Python

    Problem

    After downloading and installing Python, type import this into the Python command line and see what happens.

  • # 安装Python并在命令行模式下输入import this查看Python之禅
    >>> import this
    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    
  • INI2--Variables and Some Arithmetic

    Problem

    Given: Two positive integers aa and bb, each less than 1000.

    Return: The integer corresponding to the square of the hypotenuse of the right triangle whose legs have lengths a and b.

  • # 求边长为a和b的直角三角形的斜边的平方和
    >>> a = 3
    >>> b = 5
    >>> c = a**2 + b**2
    >>> print(c)
    34
    
  • INI3--Strings and Lists

    Problem

    Given: A string s of length at most 200 letters and four integers a, b, c and d.

    Return: The slice of this string from indices a through b and c through
    d (with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.

    # 从一个字符串中按指定索引位置提取子字符串
    >>> s = "WPedostibesprNckwovQydlBbDTX2atKZFiQWkgoppnjtPBJG8VuxVnWH5mDZIClB9Kgf3Wz5U1imodestusKXvI3Zs5gLS4W8r43ZCYQgxGeCv1eWnQveLa9PJZNo3lzjWdmDrfIN7dA1EhOC0NRVuRtHcPaHVu5snowPwjjBUNeDvH0yka33OxLCACg7d0Q."
    >>> a,b,c,d = 1,10,76,83
    >>> print(s[a:b+1])
    Pedostibes
    >>> print(s[c:d+1])
    modestus
    
  • INI4--Conditions and Loops

    Problem

    Given: Two positive integers a and b (a < b < 10000).

    Return: The sum of all odd integers from a through b, inclusively.

    # 计算a和b之间所有奇数的累加和
    >> a,b = 4908, 9604
    >>> sum = 0
    >>> for i in range(a,b+1):
    ...     if i % 2 == 1:  # 判断i是否为奇数
    ...             sum += i
    ... 
    >>> print(sum)
    17037088
    
  • INI5--Working with Files

    Problem

    Given: A file containing at most 1000 lines.

    Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.

    # 读入一个文本文件,循环输出偶数行
    >>> with open("rosalind_ini5.txt","r") as fh:
    ...     for i, line in enumerate(fh):
    ...             if i % 2 == 1: # i是从0开始的
    ...                     print(line,end="")
    ... 
    Some things in life are bad, they can really make you mad
    Other things just make you swear and curse
    When you're chewing on life's gristle, don't grumble give a whistle
    This will help things turn out for the best
    Always look on the bright side of life
    Always look on the right side of life
    If life seems jolly rotten, there's something you've forgotten
    And that's to laugh and smile and dance and sing
    When you're feeling in the dumps, don't be silly, chumps
    Just purse your lips and whistle, that's the thing
    So, always look on the bright side of death
    Just before you draw your terminal breath
    Life's a counterfeit and when you look at it
    Life's a laugh and death's the joke, it's true
    You see, it's all a show, keep them laughing as you go
    Just remember the last laugh is on you
    Always look on the bright side of life
    And always look on the right side of life
    Always look on the bright side of life
    And always look on the right side of life
    
  • INI6--Dictionaries

    Problem

    Given: A file containing at most 1000 lines.

    Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.

    # 统计一个字符串中每个单词出现的频数,以空格分割
    >>> s = "When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be"
    >>> mydict = {}
    >>> for word in s.split(" "):
    ...     if word in mydict:
    ...             mydict[word] += 1
    ...     else:
    ...             mydict[word]  = 1
    ... 
    >>> for key,value in mydict.items():
    ...     print(key + " " + str(value))
    ... 
    When 1
    I 2
    find 1
    myself 1
    in 4
    times 1
    of 11
    trouble 1
    Mother 2
    Mary 2
    comes 2
    to 3
    me 4
    Speaking 3
    words 7
    wisdom 7
    let 30
    it 36
    be 41
    And 3
    my 1
    hour 1
    darkness 1
    she 1
    is 4
    standing 1
    right 1
    front 1
    Let 6
    Whisper 4
    when 2
    the 4
    broken 1
    hearted 1
    people 1
    living 1
    world 1
    agree 1
    There 4
    will 5
    an 4
    answer 4
    For 1
    though 1
    they 2
    may 1
    parted 1
    there 2
    still 2
    a 2
    chance 1
    that 2
    see 1
    night 1
    cloudy 1
    light 1
    shines 1
    on 1
    Shine 1
    until 1
    tomorrow 1
    wake 1
    up 1
    sound 1
    music 1
    yeah 2
    # another way
    >>> words = s.split(" ")
    >>> for word in words:
    ...     mydict[word] = words.count(word)
    ...    
    >>> for key,value in mydict.items():
    ...    print(key, value)
    # another way
    >>> words = s.split(" ")
    >>> for word in words:
    ...     mydict[word] = 1 + mydict.get(word, 0) #利用字典自带的get方法
    ...    
    >>> for key,value in mydict.items():
    ...     print(key, value)
    ...
    

Bioinformatics Stronghold

  • Counting DNA Nucleotides

    Problem

    A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.

    An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."

    Given: A DNA string s of length at most 1000 nt.

    Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in s.

    # 输入一个DNA序列的字符串,统计这个DNA序列中A,C,G,T四个字符的数目,并以空格分隔输出
    >>> DNA = "TGAGTTCCGTATCGTGAAAAAGGAAGGTTAGCCAGCCTTCTCACACGATGGCTATGTTGATTTGGTCGTAGGGACACTAGATACGCTGTTTCTCTTCTTAAAGATGGAACGATGTAACGCTGGATGGAAACTGAACTAGATAAGCCAGTAGTGTAAATGTTCGTACCTGACATATCACTGTTGCAAAATTGGGGATCTATCGAAGATCAGCGCACGCGCTTCAATTGTGTTTATGGACAAGGTCTTGCGGTCGATAGGTCTTCAGACAGGCAGCGTTCCCGCGACCTTCCTCGCGATTCCTCGGATGTGGAGTGGTAGAGAACAGCGATTGGTGCCGACCTTCTGAGGAAGAACCGTGCCAGTTTGCGACATTCGTCCCAACAAATTAACCATATCACGTTAAGGTTAGCCCCCTACTAAGTACAAAGACTGGGCATCAGAAGGGCTGCCCATGTAGATGATTTGTTTTGTCAAACAAACGCGTCCGGTGGGGGCGACTTAACCACCTTCTGGCGGTGCACATTACTGGGTTCTTGAGGTGCTAATGAGCATTGCGTTGAGCGTATCAATAACGCGACTGGCGATACACCGACAGGGTATTCCGTCTGGCGCCCTGAGACGGGGTTGGGACTCCGATAAGACGTGGTGCGACCCACCGACGTGTAAAACGCAGAAGTCTCTCCTCTACATCATTGTTAAACCGGGCTAGACAGCTGCGGTCTTCTCTCATAGGCACCACCCCAGTACCCTGCCAATAGTGCATCGTGTTCTGTGAGCAAGTGTTGTTGCGGTCGCACTTGTTCTTTGTCATACAGAGGCTCCTCCTCGACTTAAGACATGTTGAAACTGTCTCGCACTGACACGCAAGGTCGGTTCACAGTTCGACCGAGCTTCAGAATGCCCAATAAAGCCTTTTCAAATAATACAACCCTGAATGCAGTCCACACCACTTTA"
    >>> mydict = {}
    >>> for item in DNA:
    ...     mydict[item] = 1 + mydict.get(item,0)
    ... 
    >>> for i in "ACGT":
    ...     print(mydict[i], end = " ")
    ... 
    237 234 240 243
    # another way
    >>> count_A, count_C, count_G, count_T = [DNA.count(nuc) for nuc in "ACGT"] #列表推导式,利用字符串自带的count方法进行计数
    >>> print(count_A,count_C,count_G,count_T)
    237 234 240 243
    # another way
    >>> count_A = DNA.count("A")
    >>> count_C = DNA.count("C")
    >>> count_G = DNA.count("G")
    >>> count_T = DNA.count("T")
    >>> print(count_A,count_C,count_G,count_T)
    237 234 240 243
    
  • Transcribing DNA into RNA

    Problem

    An RNA string is a string formed from the alphabet containing 'A', 'C', 'G', and 'U'.

    Given a DNA string t corresponding to a coding strand, its transcribed RNA string uu is formed by replacing all occurrences of 'T' in t with 'U' in u.

    Given: A DNA string t having length at most 1000 nt.

    Return: The transcribed RNA string of t.

    # 输入一个DNA序列的字符串,将其转换为RNA序列进行输出
    >>> DNA = "GTTTTGTCTAAGGCCCCGAAAATGATCTTGGACACCCCTCTCCCGATTTTCACTGCGATTTCACTTTTCATCCGGACGCCTGGTAGCGCAGGTATAAACTAAGGAGCAAGACGCTTGAAAGTTGTTAGGATAGATGCGGCCATGCCATATGCTGATATCCAGATGGTCGGTTTAACTTTATTTGGTCGAACCGTAGTACGGGGGAAACCGACCATAATGTCTATTGATTCCAGGTGTTAACCCATCAAACGCAGGCCAACATTCTCCGTATTGAGTTCACCCATCGATCTCACAGCGGTTAGGAACACGAGTGATCTAGTCGGCACTTACGCGCCGCTAGCGGGGTGCTCTACGAGTATTGCATAATGACGAGATAAAATGGGACCGTATCAATCCGGATTTCTAACGACTTGCTCCCAAAGGATACGTTACGTAATTAACTCAGGCATAGGCGTCATGGCATGTAATTTACTATGTCGGGCGCACGTAGTAATCCCTACCAAGCAGCGTAGACTAGCGAAAGTCCCCTGGTACAGTATTGTTCTTCTGAAGTCGCGTACTCCCCCAGTTCCTAGAAAAATGGGTAAATGGACTCTCGCTCGTCTCCTCGCTGGTTGGTCATGGCGTTGGGTCTGCTTTACCCCTCCAAGCCGCACGTAGCCGCCTACAGTAAATCGGGGGTGCTGGCTTACCGACGTTAAGTTGCTCCAACTACGGTGGACTGGTATCACCTACTAGTTCCCTATAGCGCGTTCTGGCTGGTTCCTAGCTGGGCACTAGGTACGGGAATTCCCCCGTACCAGACTAAAAGGAATGTAGAACGTGATGGGCATTGACACTGAACTCCTTTTTCGGGCCGGTCAGGTTGCATGGGCAACGGCCTACAACGACGTCAAACTCACCATGGCGGGGTTCCTGGGTATGCGTGCCGCTCAGGTTCTACCCTCTACCAACGAGGACGCCAACAACTGATTAC"
    >>> RNA = []
    >>> for item in DNA:
    ...     if item == "T":
    ...             item = "U"
    ...             RNA.append(item)
    ...     else:
    ...             RNA.append(item)
    ... 
    >>> print("".join(RNA))
    GUUUUGUCUAAGGCCCCGAAAAUGAUCUUGGACACCCCUCUCCCGAUUUUCACUGCGAUUUCACUUUUCAUCCGGACGCCUGGUAGCGCAGGUAUAAACUAAGGAGCAAGACGCUUGAAAGUUGUUAGGAUAGAUGCGGCCAUGCCAUAUGCUGAUAUCCAGAUGGUCGGUUUAACUUUAUUUGGUCGAACCGUAGUACGGGGGAAACCGACCAUAAUGUCUAUUGAUUCCAGGUGUUAACCCAUCAAACGCAGGCCAACAUUCUCCGUAUUGAGUUCACCCAUCGAUCUCACAGCGGUUAGGAACACGAGUGAUCUAGUCGGCACUUACGCGCCGCUAGCGGGGUGCUCUACGAGUAUUGCAUAAUGACGAGAUAAAAUGGGACCGUAUCAAUCCGGAUUUCUAACGACUUGCUCCCAAAGGAUACGUUACGUAAUUAACUCAGGCAUAGGCGUCAUGGCAUGUAAUUUACUAUGUCGGGCGCACGUAGUAAUCCCUACCAAGCAGCGUAGACUAGCGAAAGUCCCCUGGUACAGUAUUGUUCUUCUGAAGUCGCGUACUCCCCCAGUUCCUAGAAAAAUGGGUAAAUGGACUCUCGCUCGUCUCCUCGCUGGUUGGUCAUGGCGUUGGGUCUGCUUUACCCCUCCAAGCCGCACGUAGCCGCCUACAGUAAAUCGGGGGUGCUGGCUUACCGACGUUAAGUUGCUCCAACUACGGUGGACUGGUAUCACCUACUAGUUCCCUAUAGCGCGUUCUGGCUGGUUCCUAGCUGGGCACUAGGUACGGGAAUUCCCCCGUACCAGACUAAAAGGAAUGUAGAACGUGAUGGGCAUUGACACUGAACUCCUUUUUCGGGCCGGUCAGGUUGCAUGGGCAACGGCCUACAACGACGUCAAACUCACCAUGGCGGGGUUCCUGGGUAUGCGUGCCGCUCAGGUUCUACCCUCUACCAACGAGGACGCCAACAACUGAUUAC
    # another way
    >>> RNA = DNA.replace("T","U") #利用字符串自带的replace方法进行替换
    >>> print(RNA)
    
  • Complementing a Strand of DNA

    Problem

    In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'.

    The reverse complement of a DNA string ss is the string scsc formed by reversing the symbols of ss, then taking the complement of each symbol (e.g., the reverse complement of "GTCA" is "TGAC").

    Given: A DNA string s of length at most 1000 bp.

    Return: The reverse complement sc of s.

    # 输入一个DNA序列的字符串,将其转换为反向互补序列进行输出
    >>> DNA = "TGCAGCACACTGGGTCGGGCTTTTGCTTGTATTATCTCACATAAGGGTAGCATAAACCTCTTAAATCGGTCAGTTTTCCTTGGAGTAGGTTGTTACCATGGGCAAACCATTCCCAGTGGTAAGATACCGAGCAGCGTACGCGACAGGCTGTCTGATCGGACAATCGGAGTACAGAACCACCCATTTGAAACACATGCTTTAGCGCTTGGCCCTCACGAAGCCCCATACAGTCACCACCCTCTAATCTTTGGCGTTACTGTGCAGGCGCCGAGAGATTAAACTTTCAATAGTGAGGGCCTAAAACCCTATCTTAGTGACTAACCAGATATATCGTTCATTCTCATTCCGTGCCCCCCAACCTCATTATAATTACCCGAAGAGTTAACGCCTTGAGAACATCTTCACGCGGGAGCACAAGTCCGAGACTGACACCGACGCAAATGGGTGATTCTTTGGGTCACGGATCGGGATTGCCGGAAACCAATAAATTAATTACTTATACCGTCCTCGTATTCGTTCCCCCAATGCGAGCTCAAAACTATAACTAATGAAGTTTTCCATTAACCCGGTCCATACATCATCGTTTCAGGACAAAAAATTTCAGGGAGCAGCCTCACTTGTGCTAGCCTAAAAATTACATCTTTGATACACATAGTAATTGGTGATATTTAGCTGCCTCCAGCACCGGTGGAGCACAGTCATGCCGAGTGTTAGACGCCATAAGGGAGTCGGCAATATAGACGAGCCATTTTTACAAATATGCGTTGGTCGACTACCATTGCTCGGAGGACAGGCTGTTTTTACCAAAGGGCGCTCTCATGAATCCCCACACCACTAGCGAACTTGTCGAACTTC"
    >>> mydict = {"A":"T","T":"A","C":"G","G":"C"}
    >>> DNA_revc = []
    >>> for item in DNA:
    ...     if item in mydict:
    ...             DNA_revc.append(mydict[item])
    ... 
    >>> DNA_revc.reverse() # 利用列表自带的reverse函数对原字符串进行反转
    >>> print("".join(DNA_revc))
    GAAGTTCGACAAGTTCGCTAGTGGTGTGGGGATTCATGAGAGCGCCCTTTGGTAAAAACAGCCTGTCCTCCGAGCAATGGTAGTCGACCAACGCATATTTGTAAAAATGGCTCGTCTATATTGCCGACTCCCTTATGGCGTCTAACACTCGGCATGACTGTGCTCCACCGGTGCTGGAGGCAGCTAAATATCACCAATTACTATGTGTATCAAAGATGTAATTTTTAGGCTAGCACAAGTGAGGCTGCTCCCTGAAATTTTTTGTCCTGAAACGATGATGTATGGACCGGGTTAATGGAAAACTTCATTAGTTATAGTTTTGAGCTCGCATTGGGGGAACGAATACGAGGACGGTATAAGTAATTAATTTATTGGTTTCCGGCAATCCCGATCCGTGACCCAAAGAATCACCCATTTGCGTCGGTGTCAGTCTCGGACTTGTGCTCCCGCGTGAAGATGTTCTCAAGGCGTTAACTCTTCGGGTAATTATAATGAGGTTGGGGGGCACGGAATGAGAATGAACGATATATCTGGTTAGTCACTAAGATAGGGTTTTAGGCCCTCACTATTGAAAGTTTAATCTCTCGGCGCCTGCACAGTAACGCCAAAGATTAGAGGGTGGTGACTGTATGGGGCTTCGTGAGGGCCAAGCGCTAAAGCATGTGTTTCAAATGGGTGGTTCTGTACTCCGATTGTCCGATCAGACAGCCTGTCGCGTACGCTGCTCGGTATCTTACCACTGGGAATGGTTTGCCCATGGTAACAACCTACTCCAAGGAAAACTGACCGATTTAAGAGGTTTATGCTACCCTTATGTGAGATAATACAAGCAAAAGCCCGACCCAGTGTGCTGCA
    # another way
    >>> mydict = {"A":"T","T":"A","C":"G","G":"C"}
    >>> DNA_revc = []
    >>> for item in reversed(DNA): # 利用reversed函数对字符串进行反转,或着反向索引DNA[::-1]进行反转
    ...     DNA_revc.append(mydict[item])
    ...             
    >>> print("".join(DNA_revc))
    
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python第六天   类型转换 下一篇2.16

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目