Python-courses-python-exercises-games-Python-courses-

Exercie 87

Write a Python algorithm as a function which takes as parameter a tuple of two strings (s1, s2) and which returns the list of common characters to s1 and s2.
Example: if s1 = 'Python language' and s2 = 'Java Programming', the function returns:

['P', 'o', 'n', ' ', 'a', 'g']

Solution

def commonCharacters(s1 , s2):
listCommon = []
for x in s1:
if x in s2 and x not in listCommon:
listCommon.append(x)
return listCommon

s1 = "Python language"
s2 = "Java Programming"
print(commonCharacters(s1 , s2)) # the output is: ['P', 'o', 'n', ' ', 'a', 'g']

Younes Derfoufi
my-courses.net

Leave a Reply