Pattern Finder
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 91 of 92.
Challenge
MediumWrite the function find_occurrences above _ready(). It:
- Takes two strings:
textandpattern - Counts how many times
patternappears intext, including occurrences that overlap - Returns an array with three elements:
trueif the pattern was found,falseotherwise- The number of occurrences
- An array with the position where each occurrence starts
For example, if text is "abababab" and pattern is "aba", the function returns [true, 3, [0, 2, 4]], because "aba" starts at positions 0, 2 and 4.
If the pattern is not found, return [false, 0, []].
The given _ready() reads the two strings, calls your function and prints the result. Replace pass with the body of the function.
Try it yourself
extends Node
func find_occurrences(text, pattern):
# Write your code here
pass
func _ready():
var text := OS.read_string_from_stdin()
var pattern := OS.read_string_from_stdin()
# Call the function and print the result
var result = find_occurrences(text, pattern)
print(result)
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler