48.3k views
4 votes
Def shared_motif(dna_list):

This function takes in a list of DNA strings and returns the longest common substring. A substring is a contiguous sequence of characters within a string. A common substring is a substring of every element in the given list of DNA strings. Note that the number of DNA strings in the input list is not always the same! There may be multiple solutions, but you only need to return the first one.
Example:
Sample input DNA list:
("GATTACA", "TAGACCA", "ATACA"]
The returned longest common substring:
"TA"

User Vpalade
by
5.1k points

1 Answer

3 votes

Answer:

Step-by-step explanation:

The following code is written in Python, the function creates various nested loops to loop through each individual string comparing letter by letter in order to find the longest common substring. Finally, returning the substring itself.

def shared_motif(dna_list):

substr = ''

if len(dna_list) > 1 and len(dna_list[0]) > 0:

for x in range(len(dna_list[0])):

for i in range(len(dna_list[0]) - x + 1):

if i > len(substr) and all(dna_list[0][x:x + i] in y for y in dna_list):

substr = dna_list[0][x:x + i]

return substr

arr = ["GATTACA", "TAGACCA", "ATACA"]

stems = shared_motif(arr)

print(stems)

Def shared_motif(dna_list): This function takes in a list of DNA strings and returns-example-1
User Manuel Spuhler
by
5.0k points