Set Operation
# Write a Python program using functions to compute following: -
# a) List of students who play both cricket and badminton
# b) List of students who play either cricket or badminton but not both
# c) Number of students who play neither cricket nor badminton
# d) Number of students who play cricket and football but not badminton.
# A | B - Union Of Two Sets A And B
# A & B - Intersection Of Two Sets A And B
# A - B - Difference Of Two Sets A And B
# A ^ B - Symmetric Difference Of Two Sets A And B
try:
userinput = int(input("Enter Total Numbers Students In Class - "))
totalstud = []
for numc in range(userinput):
rollno = int(input(f"Enter Roll No. Of Student {numc + 1} - "))
totalstud.append(rollno)
print("List Of Roll No Of Students In The Class -", totalstud, "\n")
userinp1 = int(input("Enter The Number Of Students Who Play Cricket - "))
cricket = []
for numc in range(userinp1):
rollno1 = int(input(f"Enter Roll No. Of Student {numc + 1} Who Play Cricket - "))
cricket.append(rollno1)
print("List Of Roll No Students Who Play Cricket", cricket, "\n")
userinp2 = int(input("Enter The Number Of Students Who Play Badminton - "))
badminton = []
for numb in range(userinp2):
rollno2 = int(input(f"Enter Roll No. Of Student {numb + 1} Who Play Badminton - "))
badminton.append(rollno2)
print("List Of Roll No Students Who Play Badminton", badminton, "\n")
userinp3 = int(input("Enter The Number Of Students Who Play Football - "))
football = []
for numf in range(userinp3):
rollno3 = int(input(f"Enter Roll No. Of Student {numf + 1} Who Play Football - "))
football.append(rollno3)
print("List Of Roll No Students Who Play Football", football, "\n")
def removeDuplicate(list1):
list2 = []
for val in list1:
if val not in list2:
list2.append(val)
return f"{list2}"
# removeDuplicate([1,1,2,3,4,4])
def intersection(list1, list2):
list3 = []
for val in list1:
if val in list2:
list3.append(val)
return list3
# intersection()
def union(list1, list2):
list3 = list1 + list2
list4 = []
for i in list3:
if i not in list4:
list4.append(i)
return list4
# union()
def difference(list1, list2):
list3 = []
for val in list1:
if val not in list2:
list3.append(val)
return list3
# difference()
def symmetricDifference(list1, list2):
list3 = list1 + list2
list4 = []
for i in list3:
if i not in list4:
list4.append(i)
list5 = []
for val in list1:
if val in list2:
list5.append(val)
list6 = []
for val in list4:
if val not in list5:
list6.append(val)
return list6
# symmetricDifference()
print("List Of Students Who Play Both Cricket And Badminton -", intersection(cricket, badminton), "\n")
print("List Of Students Who Play Either Cricket Or Badminton But Not Both -",
symmetricDifference(cricket, badminton), "\n")
print("List Of Students Who Play Neither Cricket Nor Badminton -", difference(totalstud, union(cricket, badminton)),
"\n")
print("List Of Students Who Play Cricket And Football But NotBadminton -",
union(intersection(cricket, football), difference(totalstud, badminton)), "\n")
# Variations In Questions #
print("1. Set Of Roll No Students Who Play Only Cricket -", difference(cricket, union(badminton, football)))
print("2. Set Of Roll No Students Who Play Only Badminton -", difference(badminton, union(cricket, football)))
print("3. Set Of Roll No Students Who Play Only Football -", difference(football, union(cricket, badminton)))
print("4. Set Of Roll No Students Who Play Neither Football Nor Badminton -",
difference(totalstud, union(football, badminton)))
print("5. Set Of Roll No Students Who Play Neither Football Nor Cricket -",
difference(totalstud, union(football, cricket)))
print("6. Set Of Roll No Students Who Play Football And Cricket But Not Badminton -",
difference(intersection(football, cricket), badminton))
print("7. Set Of Roll No Students Who Play Badminton And Cricket But Not Football -",
difference(intersection(badminton, cricket), football))
print("8. Set Of Roll No Students Who Play Badminton And Football But Not Cricket -",
difference(intersection(badminton, football), cricket))
print("9. Set Of Roll No Students Who Play All Three Games -", union(union(cricket, badminton), football))
print("10. Set Of Roll No Roll No Students Who Play No Games -",
difference(totalstud, union(union(cricket, badminton), football)))
except Exception as e:
print("Wrong Input,Enter Integers Only.")
# Output #
# Enter Total Numbers Students In Class - abcd
# Wrong Input,Enter Integers Only.
# Enter Total Numbers Students In Class - 10
# Enter Roll No. Of Student 1 - 1
# Enter Roll No. Of Student 2 - 2
# Enter Roll No. Of Student 3 - 3
# Enter Roll No. Of Student 4 - 4
# Enter Roll No. Of Student 5 - 5
# Enter Roll No. Of Student 6 - 6
# Enter Roll No. Of Student 7 - 7
# Enter Roll No. Of Student 8 - 8
# Enter Roll No. Of Student 9 - 9
# Enter Roll No. Of Student 10 - 10
# List Of Roll No Of Students In The Class - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Enter The Number Of Students Who Play Cricket - 4
# Enter Roll No. Of Student 1 Who Play Cricket - 1
# Enter Roll No. Of Student 2 Who Play Cricket - 2
# Enter Roll No. Of Student 3 Who Play Cricket - 3
# Enter Roll No. Of Student 4 Who Play Cricket - 4
# List Of Roll No Students Who Play Cricket [1, 2, 3, 4]
# Enter The Number Of Students Who Play Badminton - 4
# Enter Roll No. Of Student 1 Who Play Badminton - 3
# Enter Roll No. Of Student 2 Who Play Badminton - 4
# Enter Roll No. Of Student 3 Who Play Badminton - 5
# Enter Roll No. Of Student 4 Who Play Badminton - 6
# List Of Roll No Students Who Play Badminton [3, 4, 5, 6]
# Enter The Number Of Students Who Play Football - 4
# Enter Roll No. Of Student 1 Who Play Football - 5
# Enter Roll No. Of Student 2 Who Play Football - 6
# Enter Roll No. Of Student 3 Who Play Football - 7
# Enter Roll No. Of Student 4 Who Play Football - 8
# List Of Roll No Students Who Play Football [5, 6, 7, 8]
# List Of Students Who Play Both Cricket And Badminton - [3, 4]
# List Of Students Who Play Either Cricket Or Badminton But Not Both - [1, 2, 5, 6]
# List Of Students Who Play Neither Cricket Nor Badminton - [7, 8, 9, 10]
# List Of Students Who Play Cricket And Football But Not Badminton - [1, 2, 7, 8, 9, 10]
# 1. Set Of Roll No Students Who Play Only Cricket - [1, 2]
# 2. Set Of Roll No Students Who Play Only Badminton - []
# 3. Set Of Roll No Students Who Play Only Football - [7, 8]
# 4. Set Of Roll No Students Who Play Neither Football Nor Badminton - [1, 2, 9, 10]
# 5. Set Of Roll No Students Who Play Neither Football Nor Cricket - [9, 10]
# 6. Set Of Roll No Students Who Play Football And Cricket But Not Badminton - []
# 7. Set Of Roll No Students Who Play Badminton And Cricket But Not Football - [3, 4]
# 8. Set Of Roll No Students Who Play Badminton And Football But Not Cricket - [5, 6]
# 9. Set Of Roll No Students Who Play All Three Games - [1, 2, 3, 4, 5, 6, 7, 8]
# 10. Set Of Roll No Students Who Play No Games - [9, 10]
# Process finished with exit code 0
Comments
Post a Comment