Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a list of positions of a substring within a string (DNA) (Python 3)

I am doing a bioinformatics course and I am trying to write a function to find all occurrences of a substring within a string.

def find_match(s, t):
  """Returns a list of all positions of a substring t in string s.

  Takes two arguments: s & t.
  """
  occurrences = []
  for i in range(len(s)-len(t)+1): # loop over alignment
    match = True
    for j in range(len(t)): # loop over characters
            if s[i+j] != t[j]:  # compare characters
                match = False   # mismatch
                break
            if match:   # allchars matched
                occurrences.append(i)

  return(occurrences)
    

print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]

The output above should exactly match the following:

[2, 4, 10]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[2, 5, 6, 15, 17, 18]

How can I fix this? Preferably without using regular expressions.

>Solution :

It looks like you badly indented the code, the

if match:

has to be outside of the inner cycle.

def find_match(s, t):
    """Returns a list of all positions of a substring t in string s.

      Takes two arguments: s & t.
    """
    occurrences = []
    for i in range(len(s)-len(t)+1): # loop over alignment
        match = True
        for j in range(len(t)): # loop over characters
            if s[i+j] != t[j]:  # compare characters
                match = False   # mismatch
                break
        if match: # <--- This shouldn't be inside the inner for cycle
            occurrences.append(i + 1)

    return occurrences
    

print(find_match("GATATATGCATATACTT", "ATAT")) # [1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 9, 9, 9, 9, 11, 11, 11, 13]
print(find_match("AUGCUUCAGAAAGGUCUUACG", "U")) # [1, 4, 5, 14, 16, 17]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading