This is the same problem statement mentioned in SET THEORY USING LIST IN PYTHON but, in this solution, we used Object-Oriented Programming Concepts like classes and class functions.
Problem Statement:- In second-year computer engineering class, group A student's play cricket, group B students play badminton and group C students play football.
Write a Python program using functions to compute the following:-
a) List of students who play both cricket and badminton
b) List of students who play either cricket or badminton or both
c) Number of students who play neither cricket nor badminton
d) Number of students who play cricket and football but not badminton.
(Note- While realizing the group, duplicate entries should be avoided, Do not use SET built-in functions)
Note:- Scroll horizontally to see the full line of code.
class Set:
def addElems(self):
self.setlist = []
n = int(input("Enter the no. of elements in the set: "))
for i in range(n):
elem = input("Enter the name of student: ")
if elem not in self.setlist:
self.setlist.append(elem)
else:
while elem in self.setlist:
print("Sorry, element is already present in the list.")
elem = input("Enter the name of student again:")
self.setlist.append(elem)
def printset(self):
return self.setlist
def union(self, B):
unionset = []
unionset.extend(self.setlist)
for i in range(len(B.setlist)):
if B.setlist[i] not in unionset:
unionset.append(B.setlist[i])
c = Set()
c.setlist = unionset
return c
def intersection(self, B):
intersectionset = []
for i in range(len(self.setlist)):
if self.setlist[i] in B.setlist:
intersectionset.append(self.setlist[i])
c = Set()
c.setlist = intersectionset
return c
def difference(self, B):
differenceset = []
for i in range(len(self.setlist)):
if self.setlist[i] not in B.setlist:
differenceset.append(self.setlist[i])
c = Set()
c.setlist = differenceset
return c
def symmetricdifference(self, B):
symmetricdifferenceset = []
for i in range(len(self.setlist)):
if self.setlist[i] not in self.intersection(B).setlist:
symmetricdifferenceset.append(self.setlist[i])
for i in range(len(B.setlist)):
if B.setlist[i] not in self.intersection(B).setlist:
symmetricdifferenceset.append(B.setlist[i])
c = Set()
c.setlist = symmetricdifferenceset
return c
# set A for Cricket
print("Cricket")
A = Set()
A.addElems()
print("List of Students who play football is: ", A.printset())
# set B for Badminton
print("\nBadminton")
B = Set()
B.addElems()
print("List of Students who play badminton is: ", B.printset())
# set C for Football
print("\nFootball")
C = Set()
C.addElems()
print("List of Students who play Football is: ", C.printset())
# building universal set U
U = A.union(B.union(C))
print("\nUniversal set is: ", U.printset())
print("\nList of Students play football is: ", A.printset())
print("List of Students who play badminton is: ", B.printset())
print("List of Students who play Football is: ", C.printset())
# Q1 list of students who play both cricket and badminton
AnB = A.intersection(B)
print("\nList of students who play both cricket and badminton: ", AnB.printset())
# Q2 List of students who play either cricket or badminton but not both
AsymmB = A.symmetricdifference(B)
print("\n\nList of students who play either cricket or badminton but not both: ", AsymmB.printset())
# Q3 Number of students who play neither cricket nor badminton
D = (U.difference(A)).difference(B)
print("\n\nNumber of students who play neither cricket nor badminton: ", len(D.setlist))
print("List of students who play neither cricket nor badminton: ", D.printset())
# Q4 Number of students who play cricket and football but not badminton
E = (A.union(C)).difference(B)
print("\n\nNumber of students who play cricket and football but not badminton: ", len(E.setlist))
print("List of students who play cricket and football but not badminton: ", E.printset())
Comments
Post a Comment