samedi 26 novembre 2016

Can this be written more 'pythonic'?

I can often solve excercises that my teacher gives me, altough not always the most 'pythonic'. Could anyone improve my code? Maybe the 'if' statement could be written on less lines?

def omgekeerdComplement(string):
    """
    Writes the complement and the reverse of the given DNA string

    >>> omgekeerdComplement('GATATC')
    'GATATC'
    >>> omgekeerdComplement('GCATGC')
    'GCATGC'
    """

   newstr = ''
    for i in range(0, len(string)):
        if string[i] == 'G': newstr += 'C'
        elif string[i] == 'C': newstr += 'G'
        elif string[i] == 'A': newstr +='T'
        else: newstr += 'A'
    return(str(newstr[::-1]))

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Cheers!

Aucun commentaire:

Enregistrer un commentaire