questions:
In second-year computer engineering class, group A students 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 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.
(Note- While realizing the group, duplicate entries should be avoided, Do not use SET built-in functions)
Code:
def accept_set(A, Str):
n = int(input("Enter the total no. of student who play %s : " % Str))
for i in range(n):
x = input("Enter the name of student %d who play %s : " % ((i + 1), Str))
A.append(x)
print("Set accepted successfully")
def display_set(A, Str):
n = len(A)
if n == 0:
print("\nGroup of Students who play %s = { }" % Str)
else:
print("\nGroup of Students who play %s = {" % Str, end=' ')
for i in range(n - 1):
print("%s," % A[i], end=' ')
print("%s }" % A[n - 1])
def search_set(A, X):
n = len(A)
for i in range(n):
if A[i] == X:
return 1
return 0
def find_intersection_set(A, B, C):
for i in range(len(A)):
flag = search_set(B, A[i])
if flag == 1:
C.append(A[i])
def find_difference_set(A, B, C):
for i in range(len(A)):
flag = search_set(B, A[i])
if flag == 0:
C.append(A[i])
def find_union_set(A, B, C):
for i in range(len(A)):
C.append(A[i])
for i in range(len(B)):
flag = search_set(A, B[i])
if flag == 0:
C.append(B[i])
def Main():
Group_A = []
Group_B = []
Group_C = []
while True:
print("\t1 : Accept the Information")
print("\t2 : List of students who play both cricket and badminton")
print("\t3 : List of students who play either cricket or badminton but not both")
print("\t4 : Number of students who play neither cricket nor badminton")
print("\t5 : Number of students who play cricket and football but not badminton")
print("\t6 : Exit")
ch = int(input("Enter your choice : "))
Group_R = []
if ch == 6:
print("End of Program")
break
elif ch == 1:
accept_set(Group_A, "Cricket")
accept_set(Group_B, "Badminton")
accept_set(Group_C, "Football")
display_set(Group_A, "Cricket")
display_set(Group_B, "Badminton")
display_set(Group_C, "Football")
elif ch == 2:
display_set(Group_A, "Cricket")
display_set(Group_B, "Badminton")
find_intersection_set(Group_A, Group_B, Group_R)
display_set(Group_R, " both Cricket and Badminton")
elif ch == 3:
display_set(Group_A, "Cricket")
display_set(Group_B, "Badminton")
R1 = []
find_union_set(Group_A, Group_B, R1)
R2 = []
find_intersection_set(Group_A, Group_B, R2)
find_difference_set(R1, R2, Group_R)
display_set(Group_R, " either cricket or badminton but not both")
elif ch == 4:
display_set(Group_A, "Cricket")
display_set(Group_B, "Badminton")
display_set(Group_C, "Football")
R1 = []
find_union_set(Group_A, Group_B, R1)
find_difference_set(Group_C, R1, Group_R)
display_set(Group_R, " neither cricket nor badminton")
print("Number of students who play neither cricket nor badminton = %s" % len(Group_R))
elif ch == 5:
display_set(Group_A, "Cricket")
display_set(Group_C, "Football")
display_set(Group_B, "Badminton")
R1 = []
find_intersection_set(Group_A, Group_C, R1)
find_difference_set(R1, Group_B, Group_R)
display_set(Group_R, "cricket and football but not badminton")
print("Number of students who play cricket and football but not badminton = %s" % len(Group_R))
else:
print("Wrong choice entered !! Try again")
Main()
quit()
Output:
1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 1 Enter the total no. of student who play Cricket : 5 Enter the name of student 1 who play Cricket : Player1 Enter the name of student 2 who play Cricket : Player2 Enter the name of student 3 who play Cricket : Player3 Enter the name of student 4 who play Cricket : Player4 Enter the name of student 5 who play Cricket : Player5 Set accepted successfully Enter the total no. of student who play Badminton : 4 Enter the name of student 1 who play Badminton : Player2 Enter the name of student 2 who play Badminton : BPlayer2 Enter the name of student 3 who play Badminton : BPlayer3 Enter the name of student 4 who play Badminton : BPlayer4 Set accepted successfully Enter the total no. of student who play Football : 5 Enter the name of student 1 who play Football : FPlayer1 Enter the name of student 2 who play Football : FPlayer2 Enter the name of student 3 who play Football : BPlayer3 Enter the name of student 4 who play Football : Player4 Enter the name of student 5 who play Football : FPlayer5 Set accepted successfully Group of Students who play Cricket = { Player1, Player2, Player3, Player4, Player5 } Group of Students who play Badminton = { Player2, BPlayer1, BPlayer2, BPlayer4 } Group of Students who play Football = { FPlayer1, FPlayer2, BPlayer3, Player4, FPlayer5 } 1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 2 Group of Students who play Cricket = { Player1, Player2, Player3, Player4, Player5 } Group of Students who play Badminton = { Player2, BPlayer1, BPlayer2, BPlayer4 } Group of Students who play both Cricket and Badminton = { Player2 } 1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 3 Group of Students who play Cricket = { Player1, Player2, Player3, Player4, Player5 } Group of Students who play Badminton = { Player2, BPlayer1, BPlayer2, BPlayer4 } Group of Students who play either cricket or badminton but not both = { Player1, Player3, Player4, Player5, BPlayer1, BPlayer2, BPlayer4 } 1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 4 Group of Students who play Cricket = { Player1, Player2, Player3, Player4, Player5 } Group of Students who play Badminton = { Player2, BPlayer1, BPlayer2, BPlayer4 } Group of Students who play Football = { FPlayer1, FPlayer2, BPlayer3, Player4, FPlayer5 } Group of Students who play neither cricket nor badminton = { FPlayer1, FPlayer2, BPlayer3, FPlayer5 } Number of students who play neither cricket nor badminton = 4 1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 5 Group of Students who play Cricket = { Player1, Player2, Player3, Player4, Player5 } Group of Students who play Football = { FPlayer1, FPlayer2, BPlayer3, Player4, FPlayer5 } Group of Students who play Badminton = { Player2, BPlayer1, BPlayer2, BPlayer4 } Group of Students who play cricket and football but not badminton = { Player4 } Number of students who play cricket and football but not badminton = 1 1 : Accept the Information 2 : List of students who play both cricket and badminton 3 : List of students who play either cricket or badminton but not both 4 : Number of students who play neither cricket nor badminton 5 : Number of students who play cricket and football but not badminton 6 : Exit Enter your choice : 6 End of Program Process finished with exit code 0