Python-courses-python-exercises-python-dictionary

Exercise 72 **

Write a python algorithm to examine if an occurrence is present within a given string.

Solution

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

# initialize counter
counter = 0
# searching the occurrence occ within the string s
for i in range(0 , n-m):
if s[i : m + i] == occ:
counter = counter + 1
if counter > 0:
return True
else:
return False

# Example:
s = "Python is the most popular programming language"
occ1 = "most"
occ2 = "algorithm"
print(examineOccurrence(s, occ1)) # display: True
print(examineOccurrence(s, occ2)) # display: False

Younes Derfoufi
my-courses.net

Leave a Reply