0
0
DSA Pythonprogramming~10 mins

String Pattern Matching Naive in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Apattern
Btext
Ctext[i]
Dpattern[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using text instead of pattern for comparison.
Comparing single characters instead of substrings.
2fill in blank
medium

Complete 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'
Alen(pattern) - 1
Blen(text)
Clen(text) - 1
Dlen(pattern)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) instead of len(pattern) in the range.
Off-by-one errors in the loop range.
3fill in blank
hard

Fix 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'
Apattern[j]
Bpattern[i]
Ctext[j]
Dtext[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing text characters with wrong indices.
Using text[i] instead of pattern[j].
4fill in blank
hard

Fill 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'
Alen(pattern)
Blen(text)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) in inner loop.
Incorrect loop ranges causing index errors.
5fill in blank
hard

Fill 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'
Alen(pattern)
Blen(text)
Cpattern
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(text) instead of len(pattern) in loop or slice.
Comparing substring with text instead of pattern.