MySetOperations is a collection of functions, on which the following set operati
ID: 3785144 • Letter: M
Question
MySetOperations is a collection of functions, on which the following set operations are defined:
myIsEmpty(A): Returns T if the set A is empty (A = ), F otherwise. myDisjoint(A,B): Returns T if intersection of the set A and set B is non-empty (A B ), F otherwise.
myIntersection(A,B): Returns the intersection of the set A and set B (A B).
myUnion(A,B): Returns the union of the set A and set B (A B).
myDifference(A,B): Returns the complement of the set A and set B (A B).
mySymDifference(A,B): Returns the symmetric difference of the set A and set B (A B).
Write a new collection of MySetOperations in Python only using the standard flow Python’s instructions and the set build-in operations: in, not in, and add.
Explanation / Answer
class MySetOperations:
def myIsEmpty(self,A):
if len(A):
return False
return True
def myDisjoint(self,A,B):
C= A&B
if len(C):
return False
return True
def myIntersection(self,A,B):
return A&B
def myUnion(self,A,B):
return A | B
def myDifference(self,A,B):
return A-B
def mySymDifference(self,A,B):
return A^B
ob=MySetOperations()
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
print ob.myIsEmpty(A)
print ob.myDisjoint(A,B)
print ob.myIntersection(A,B)
print ob.myUnion(A,B)
print ob.myDifference(A,B)
print ob.mySymDifference(A,B)
===================================
Output:
akshay@akshay-Inspiron-3537:~$ python setop.py
False
False
set([4, 5])
set([1, 2, 3, 4, 5, 6, 7, 8])
set([1, 2, 3])
set([1, 2, 3, 6, 7, 8])
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.