"Add a function to the sets module called issubset with a function signature issubset(set1, set2). Your function should return True if every element in set1 appears in set2. Make sure you document your function fully and remember to include preconditions and postconditions. Also, include some testing for your new function."

Can anybody help me with approching this question?


Here would be one approach ...

# Python27

# create two different test sets
list1 = list('abcde')
list2 = list('acdef')
set1 = set(list1)
set2 = set(list2)


set(['a', 'c', 'b', 'e', 'd'])
set(['a', 'c', 'e', 'd', 'f'])

match = True
# now iterate through set1 and print any items that are not in set2
for item in set1:
    if item not in set2:
        print(item)  # test
        match = False

# dito for set2
for item in set2:
    if item not in set1:
        print(item)  # test
        match = False

# the look of sets have changed with Python3

myset = set('abcd')


result with Python2 -->
set(['a', 'c', 'b', 'd'])
result with Python3 -->
{'a', 'c', 'b', 'd'}

print(len(myset))     # 4

print('c' in myset) # True

This question has already been answered. Start a new discussion instead.