Coding Challenge: Implement Pattern Count
Use knowledge from the last lesson to perform the following exercise.
We'll cover the following...
Pattern frequency
Pattern Count Problem
Problem overview:
Provided with two strings, Text and Pattern, find the number of times Pattern is repeated in Text.
Input: Strings Text and Pattern.
Output: Count(Text, Pattern).
Sample dataset:
GCGCG
GCG
Sample output:
2
Press + to interact
def PatternCount(Text, Pattern):count = 0# Write your code herereturn count
Solution explanation
- Line 2: We define the variable
countfor storing the pattern count. - Lines 3–5: We iterate over
Textto find the providedPatternin that text.- Line 4: We check if the substring from
ito the length of the pattern is equal toPattern. - Line 5: We increment the
countif the condition is met.
- Line 4: We check if the substring from
Ask