## Sunday, September 5, 2021

Solution Exercise 73: Python algorithme which determines the List of position of a pattern in a given Texte

#### Exercise 73

Write a function which takes as parameter a variable of type string s and another string T, and returns the list of positions of a pattern s in the string T. The function must return the empty list if the pattern s does not appear in the text T.

#### Solution

``````#function which determines the list of positions of s in the text T
def listPositions (T, s):
# initializing the list of s occurrences in T
lPosition = []
for i in range (0, len (T)):
# we test if the occurrence s is at position i
if T [i: len (s) + i] == s:
lPosition.append (i)
return lPosition
# Example
T = "Python is the most popular language and the most used language"
s = 'most'
print (listPositions (T, s)) # output: [14, 44]``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 