Experiment No 1
Experiment No 1
Problem Statement:-
In second year computer engineering class, group A student’s play cricket, group Bstudents
play badminton and group C students play football.
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.
(Note- While realizing the group, duplicate entries should be avoided, Do not use SET
built-in functions)
>>>A = {1, 2, 3, 4, 5}
>>>B = {4, 5, 6, 7, 8}
>>>print(A | B) # OR by using union()
>>>print(A.union(B)) # output of both will be same {1, 2, 3, 4, 5, 6, 7, 8}
2. Set Intersection:-
This method takes as argument sets, and returns the common items in all the sets.
Intersection is performed using & operator. Same can be accomplished using the
method intersection().
>>>A = {1, 2, 3, 4, 5}
>>>B = {4, 5, 6, 7, 8}
>>>print(A & B) # OR by using intersection()
>>>print(A.intersection(B)) # output of both will be same {4, 5}
3. Set Difference
The difference() method returns the difference of two or more sets. It returns
as a set.
Difference of A and B (A - B) is a set of elements that are only in A but not in
B. Similarly, B - A is a set of element in B but not in A.
Difference is performed using - operator. Same can be accomplished using the
method difference().
>>>A = {1, 2, 3, 4, 5}
>>>B = {4, 5, 6, 7, 8}
>>>print(A - B) # OR by using difference()
>>>print(A.difference(B)) # output of both will be same {1, 2, 3}
>>>A = {1, 2, 3, 4, 5}
>>>B = {4, 5, 6, 7, 8}
>>>print(A ^ B) # OR by using symmetric_difference()
>>>print(A.symmetric_difference(B)) # output of both will be same {1, 2, 3, 6, 7, 8}
Algorithm:-
1) Start.
2) Declare variables such as SEComp,Cricket,Football,Badminton,ch.
3) Input no of student in SEComp along with their names.
4) Input no.of students playing Cricket along with their names
5) Input no of students playing Football along with their names
6) Input no of students playing Badminton along with their names.
7) Print menu 1) List of student who plays cricket and badminton.
2) List of student who plays either cricket or badminton but not both.
3) List of student who plays neither cricket nor badminton.
4) No. of student who plays cricket and football but not badminton.
5) Exit.
ch=Input Enter your choice
8) If ch==1 then call function CB
Input value of a do you want to continue (y/n).
If a==’y’ then go to step 7.
else,thank you for using program.
9) else if ch==2,then call function eCeB
Input value of a do you want to continue (y/n).
If a==’y’ then go to step 7.
else ,thank you for using program.
10) else if ch==3 then call function nCnB.
Input value of a, do you want to continue (y/n).
If a==’y’ then go to step 7.
else, thank you for using program.
11) else if ch==4 then call function CFnB
Input value of a , do you want to continue (y/n).
If a==’y’ ,then go to step 7.
else,thank you for using program.
12) else if ch==5 then ,go to step 13.
else,print wrong choice .
Input value of a, do you want to continue (y/n)
if a==’y’ then go to step 7.
else,thank you for using program.
13) Stop.
Start
Flowchart:-
Declare variables such as SEComp,Cricket,Football,Badminton,ch,a
Yes
if ch === 1 CB(Cricket,Badmiton)
Yes No
thank you for
if a === ‘y’
using program.
Yes
if ch === 2 eCeB(Cricket,Badmiton)
No
a=Input value of a do you want to continue (y/n).
Yes
if ch === 3
nCnB(Cricket,Badmiton)
No
a=Input value of a do you want to continue
(y/n).
Yes
if ch ===4
CFnB(Cricket,Footbal ,Badmiton)
No
a=Input value of a do you want to continue (y/n).
Yes No
if a === ‘y’ thank you for
using program.
Stop
Conclusion:
By this way, we can learn how to implement various set operations.