Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the pattern exists in the text using the 'in' keyword.
DSA Python
text = "hello world" pattern = "world" if pattern [1] text: print("Pattern found") else: print("Pattern not found")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'in' to check substring.
Using 'is' which checks object identity, not substring.
✗ Incorrect
The 'in' keyword checks if the pattern is a substring of the text.
2fill in blank
mediumComplete the code to find the first index of the pattern in the text using the find() method.
DSA Python
text = "abracadabra" pattern = "cad" index = text.[1](pattern) print(index)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' which raises an error if substring not found.
Using non-existent methods like 'search' or 'locate'.
✗ Incorrect
The find() method returns the lowest index of the substring if found, else -1.
3fill in blank
hardFix the error in the code to correctly count occurrences of the pattern in the text.
DSA Python
text = "banana" pattern = "ana" count = text.[1](pattern) print(count)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() which returns only the first index.
Using index() which raises error if substring not found.
✗ Incorrect
The count() method returns the number of non-overlapping occurrences of the substring.
4fill in blank
hardFill both blanks to create a dictionary of patterns and their counts from a list of words.
DSA Python
words = ["apple", "banana", "apple", "cherry"] pattern_counts = {word: words.[1](word) for word in words if "a" [2] word} print(pattern_counts)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() instead of count() for counting.
Using 'not in' instead of 'in' to filter words.
✗ Incorrect
Use count() to count occurrences and 'in' to check if 'a' is in the word.
5fill in blank
hardFill all three blanks to create a dictionary of uppercase patterns and their first index in text if found.
DSA Python
text = "hello world" patterns = ["he", "wo", "or"] result = {pattern.[1](): text.[2](pattern) for pattern in patterns if text.[3](pattern) != -1} print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() instead of find() for index.
Using upper() incorrectly or missing it.
✗ Incorrect
Use upper() to uppercase pattern, find() to get index, and find() to check presence.