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
USING PYTHON: Write a cencrypt() function that takes a arbitrary length string and an integer as...
USING PYTHON: Write a cencrypt() function that takes a arbitrary length string and an integer as inputs and returns a string as output. The cencrypt() function you write in lab today needs to work on strings that contain upper-case letters, lower-case letters, or both. The output string should be all upper-case letters. (The Romans of Caesar's time used only what today we call upper-case letters.) So a call to cencrypt("Caesar", 1) should return (return not print) the string “DBFTBS” and...
Write a Python function that computes the frequency of characters in a string s. The function...
Write a Python function that computes the frequency of characters in a string s. The function should return a map where the key is the character and the value is the count. Make sure you include a main function and include comments to document your code.
Python Write function stringCount() that takes two string inputs—a file name and a target string— and...
Python Write function stringCount() that takes two string inputs—a file name and a target string— and returns the number of occurrences of the target string in the file. >>> stringCount('example.txt', 'line') 4
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
using python Write function stringCount() that takes two string inputs—a file name and a target string—...
using python Write function stringCount() that takes two string inputs—a file name and a target string— and returns the number of occurrences of the target string in the file. >>> stringCount('example.txt', 'line') 4
[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).
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT