0% found this document useful (0 votes)
21 views3 pages

Experiment No 1

experiment

Uploaded by

mspande1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Experiment No 1

experiment

Uploaded by

mspande1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment No-01

Title:- Set Operations


Objectives:-
1. Understand the implementation of array data structure.
2. Understand the implementation of set with various operations like
union, intersection, difference and symmetric difference.

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)

Theory- Basic Set operations:-


Sets can be used to carry out mathematical set operations like union, intersection,
difference and symmetric difference. We can do this with operators or methods.

1. Set Union:-
This method performs the union operation on two or more Python sets. What it
does is it returns all the items that are in any of those sets.
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.

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.
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}

4. Set Symmetric Difference


This method returns all the items that are unique to each set.
Symmetric Difference of A and B is a set of elements in both A and B except
those that are common in both.

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 to find out list of students who play both cricket and badminton
Assumptions:
List1 is list of students playing cricket
List2 is list of students playing badminton

1.Start
2.For every value in list1 do following
i)Check if it matches with any value of list2
if yes, put it in list3
3.Print list3 as resulting list.
4.stop.

Algorithm to find out list of students who play either cricket or badminton but not both
Assumptions:
List1 is list of students playing cricket
List2 is list of students playing badminton

1.Start
2.Find difference1=(list1-list2) using difference function
3.Find difference 2= (list2-list1) using difference function
4. Find union of difference 1 and difference 2 and print it as result
5.stop.
Algorithm to number of students who play neither cricket nor badminton

List1 is list of all SE comp students


List2 is list of students playing cricket
List3 is list of students playing badminton

1.Start
2.Find union of list2 and list3
3.Find difference = list1- union(list2 and list3)
4. print calculated difference as result
5.stop.

Algorithm to find out number of students who play cricket and football but not
badminton
List1 is list of all SE comp cricket
List2 is list of students playing football
List3 is list of students playing badminton

1.Start
2.Find intersection of list1 and list2
3.Find difference = (intersection(list1 and list2) – list3)
4. print calculated difference as result.
5.stop.

Conclusion: Thus we have learned how to implement various set operation by using list.

You might also like