Exercise 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 characters in s1 and not in s2. Example: if s1 = 'Python language' and s2 = 'Java Programming', the function returns the list:
['y', 't', 'h']
Solution
def differenceS1S2(s1 , s2): listDifference = [] for x in s1: if x not in s2 and x not in listDifference: listDifference.append(x) return listDifference s1 = "Python Programming" s2 = "Java Programming" print(differenceS1S2(s1 , s2)) # the output is: ['y', 't', 'h']
Younes Derfoufi
my-courses.net
my-courses.net