Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the pattern matches the text at position i.
DSA Python
if text[i:i+len(pattern)] == [1]: print(f"Pattern found at index {i}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using text instead of pattern for comparison.
Comparing single characters instead of substrings.
✗ Incorrect
We compare the substring of text starting at i with the pattern to check for a match.
2fill in blank
mediumComplete the code to iterate over all possible starting positions in the text.
DSA Python
for i in range(len(text) - [1] + 1): # check pattern match here
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) instead of len(pattern) in the range.
Off-by-one errors in the loop range.
✗ Incorrect
We iterate until the last possible start where the pattern can fit inside the text.
3fill in blank
hardFix the error in the code to correctly check for pattern match character by character.
DSA Python
match = True for j in range(len(pattern)): if text[i+j] != [1]: match = False break
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing text characters with wrong indices.
Using text[i] instead of pattern[j].
✗ Incorrect
We compare each character of the pattern with the corresponding character in the text.
4fill in blank
hardFill both blanks to complete the naive pattern matching function.
DSA Python
def naive_pattern_search(text, pattern): for i in range(len(text) - [1] + 1): match = True for j in range([2]): if text[i+j] != pattern[j]: match = False break if match: print(f"Pattern found at index {i}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) in inner loop.
Incorrect loop ranges causing index errors.
✗ Incorrect
The outer loop runs until text length minus pattern length plus one, and inner loop runs over pattern length.
5fill in blank
hardFill all three blanks to create a dictionary of pattern occurrences with their start indices.
DSA Python
def find_pattern_positions(text, pattern): positions = [] for i in range(len(text) - [1] + 1): if text[i:i+[2]] == [3]: positions.append(i) return positions
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) instead of len(pattern) in loop or slice.
Comparing substring with text instead of pattern.
✗ Incorrect
We use len(pattern) to limit the loop and substring length, and compare with the pattern.