# (Uses str.find.) def get_indices(text, pattern): '''Return a list containing the indices where str pattern appears in str text. Overlapping instances of pattern should be counted. For example, get_indices("abcabcabc", "bc") --> [1, 4, 7] get_indices("bbbbbbb", "bb") --> [0, 1, 2, 3, 4, 5]''' # Accumulate the indices of occurrences here. indices = [] # The position of the next occurrence of pattern within text. index = text.find(pattern, 0) # Keep looking until there are no more occurrences. while index != -1: # Record this occurrence. indices.append(index) # Advance past this occurrence and look for the next one. index = text.find(pattern, index + 1) return indices # (Does not use str.find.) def basic_stringmatch(text, pattern): '''Find all instances of the string pattern in the string text. Return a list containing the starting indices of all matches. This version iterates through text one character at a time, checking if the pattern matches starting at that character. It checks the entire pattern at each character.''' matches = [] # Iterate through text for index in range(len(text) - len(pattern) + 1): # Check if the pattern matches starting at this character match = True for p_index in range(len(pattern)): if text[index + p_index] != pattern[p_index]: match = False if match: matches.append(index) return matches def early_exit_stringmatch(text, pattern): '''Find all instances of the string pattern in the string text. Return a list containing the starting indices of all matches. This version iterates through text one character at a time, checking if the pattern matches starting at that character. It stops checking the pattern when a mismatch is detected.''' matches = [] # Iterate through text for index in range(len(text) - len(pattern) + 1): # Check if the pattern matches starting at this character p_index = 0 while p_index < len(pattern) and \ text[index + p_index] == pattern[p_index]: p_index = p_index + 1 # The pattern matches if the previous loop iterated through # the entire pattern. if p_index == len(pattern): matches.append(index) return matches def main(): print(get_indices("abcabcabc", "bc"), "sb", [1,4,7]) print(get_indices("bbbbbbb", "bb"), "sb", [0,1,2,3,4,5]) print(get_indices("bbbbbbb", "aa"), "sb", []) print(get_indices("abc", "abcde"), "sb", []) print(get_indices("", ""), "sb", [0]) print(get_indices("abcabcabc", "ab"), "sb", [0,3,6]) print(basic_stringmatch("CTACAATATATCGTATCATATCC","ATATC"), "sb",[7,17]) print(basic_stringmatch("ATATAT","AT"), "sb", [0,2,4]) print(early_exit_stringmatch("CTACAATATATCGTATCATATCC","ATATC"), "sb",[7,17]) print(early_exit_stringmatch("ATATAT","AT"), "sb", [0,2,4]) main()