features-Python-courses-python-exercises-python-oop

Exercise 85

Write a python algorithm which determines the set of all characters that make up a string s. Example if s = "Python programming", the algorithm returns the set:

{'p', 'i', '', 'r', 'o', 'm', 't', 'a', 'h', 'y', 'P', 'g' , 'n'}

Solution

# create a function which returns all the characters that make up a string s
def characterSet (s):
# initialization of the set of characters
Set = set ({})

# iterate over all characters of the string s
for x in s:
Set.add (x)
return Set

# Example
s = "Python programming"
print (characterSet (s))
# output: {'p', 'i', '', 'r', 'o', 'm', 't', 'a', 'h', 'y', 'P', 'g' , 'n'}

Younes Derfoufi
my-courses.net

Leave a Reply