Exercise 75

Write a python algorithm which determines the list of all common characters to two strings s1 and s2 without repetition. Example if s1 = "Python language" and s2 = "Programming", the algorithm returns the liste ['P', 'o', 'n', 'a', 'g'] ( the charcter 'g' must be added only once even if shared twice)


def commonCharacters(s1 , s2):
# initializing the list of common characters
listCommon = []
for x in s1:
if x in s2 and x not in listCommon:
return listCommon

# Example
s1 = "Python language"
s2 = "Programming"
print("List of common characters : ", commonCharacters(s1 , s2))
# The output is : List of common characters : ['P', 'o', 'n', 'a', 'g']

Younes Derfoufi

Leave a Reply