Question

Python: How would I write a function that takes a string representing a DNA sequence, and...

Python: How would I write a function that takes a string representing a DNA sequence, and return a string representing the complement ('A' and 'T' are complements of each other, as are 'C' and 'G'). The hints given were to use a dictionary to map a symbol A to its complement T, and to use join() rather than string addition.

    def dna_complement(text: str) -> str:

assert(dna_complement('AAAACCCGGT') == 'ACCGGGTTTT')
assert dna_complement('AcgTTTcAgCCC') == 'GGGCTGAAACGT'

Homework Answers

Answer #1

Solution:-

Here is the Python code for the given requirement. Comments have been added in the code for better explanation.

def dna_complement(text):
    # Creating a dictionary mapping the symbols with its complement
    dic = {'A':'T','T':'A','C':'G','G':'C'}
    
    # define an empty list
    ls = []
    
    # for each character in the input string
    for i in range(len(text)):
        # if the upper case of the character is a key in a dictionary
        if text[i].upper() in dic.keys():
            # append the value of that key to the list
            ls.append(dic.get(text[i].upper()))
    
    # list ls will contain a list with all characters complement. 
    # we use the join function to createa string from that list.
    # and then use the slicing shortcut [::-1] to reverse that string and return it.         
    return((''.join(ls))[::-1])

if __name__ == '__main__':
    print('The complement of DNA string AAAACCCGGT is ',dna_complement("AAAACCCGGT"))
    print('The complement of DNA string AcgTTTcAgCCC is ',dna_complement("AcgTTTcAgCCC"))

The output is as follows:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
write a function call character_thing (string, character) that takes in a string and a character. The...
write a function call character_thing (string, character) that takes in a string and a character. The function will return the number of times the character appears in the string. please be in python
[Python] Write a function named "total_population" that takes a string then a list as parameters where...
[Python] Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a list containing 3 strings as elements representing the CountryCode, CityName, and Region in this order. Return the total population of all cities in the list. Note that the city must match the country, name, and...
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Write a function clean(dna) that returns a new DNA string in which every character that is...
Write a function clean(dna) that returns a new DNA string in which every character that is not an A, C, G, or T is replaced with an N. For example, clean('goat') should return the string 'gnat'. You can assume dna is all lowercase, but don't assume anything about the nature of the wrong characters (e.g. they could even have been accidentally transcribed as numbers). No importing Must use for loop
Write a python function called unique words that takes a phrase as an input string and...
Write a python function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.