"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?

Thanks

Recommended Answers

All 5 Replies

Did you solve this by yourself, or would you have any specific quiestion?

Here would be one approach ...

# Python27

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

print(set1)
print(set2)

'''
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

print(match)
# the look of sets have changed with Python3

myset = set('abcd')

print(myset)

"""
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 is dead thread, OP visitted one month ago (first and) last time.

Still a thread that other people visit and are interested in the topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.