Pattern Finder
Part of the Fundamentals section of Coddy's Python journey — lesson 76 of 77.
Challenge
MediumCreate a function named find_occurrences that:
- Takes two string arguments:
textandpattern - Counts how many times
patternappears intext, including overlapping occurrences - Returns a tuple containing:
- A boolean indicating if the pattern was found (True/False)
- The number of occurrences of the pattern
- A list of starting positions where the pattern was found
For example, if text is "abababab" and pattern is "aba", your function should return (True, 3, [0, 2, 4]), since "aba" appears at positions 0, 2, and 4.
If the pattern is not found, return (False, 0, []).
About the <strong>pass</strong> keyword: You'll see pass in the default code. It's a Python keyword that means "do nothing" and is used as a placeholder when Python requires an indented code block (like inside a function). You should replace pass with your actual function code.
Try it yourself
def find_occurrences(text, pattern):
# Write your code here
pass
# Read input
text = input()
pattern = input()
# Call your function and print the result
result = find_occurrences(text, pattern)
print(result)All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2