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)
Solution
def commonCharacters(s1 , s2):
# initializing the list of common characters
listCommon = []
for x in s1:
if x in s2 and x not in listCommon:
listCommon.append(x)
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
my-courses.net
my-courses.net