THE CHALLENGEFIRST_OCCURRENCE — Find a pattern in a text.
You are given two strings: haystack, then needle. Find the index of the first occurrence of needle in haystack, or -1 if needle does not appear.
Input:
- line 1: haystack
- line 2: needle
Output:
an integer: the index of the first occurrence, or -1.
Example:
sadbutsad
sad
gives
0
How to break it down:
1. premiere_occurrence(texte, motif): goes through each position of texte and tests whether motif starts there (or uses texte.find(motif)).
2. Read both lines and print the result.
Challenge: find() and index() are not allowed - compare slices texte[i:i+len(motif)] by hand.
Checks (verified automatically before your code runs):
- At least 1 function definition.
- At least 1 call to premiere_occurrence().
- Do not use find().
- Do not use index().
- At least 1 slice like s[a:b].