## Sunday, November 1, 2020

Solution Exercise 78 : python algorithm that find all index of an occurrence in given string #### Exercise 78 **

Write a python algorithm which returns the list of all index where an occurrence occ is found within a given string s without using any predefined method as find(), rfind() , index()... The function returns the empty list [], if occ does not exist in s.
Example:
s = "Python is an interpreted language. Python is open source. Python is easy to learn"
occ = "Python" , the algorithm returns the list: [0, 35, 58]

#### Solution

``````def indexOfOccurrence(s , occ):
# getting the length of the occurrence occ and the len of the string s
m = len(occ)
n = len(s)

# initializing the list of index
listIndex = []
# searching all indx of occurrence occ within the string s
for i in range(0 , n-m):
if s[i : m + i] == occ:
listIndex.append(i)
return listIndex

# Example:
s = "Python is an interpreted language. Python is open source. Python is easy to learn"
occ = "Python"
print(indexOfOccurrence(s, occ))
# The output is: [0, 35, 58]``````

Younes Derfoufi
my-courses.net 